• WebService--cxf


    cxf 版本:3.1.10

    开发环境:jdk 1.6

    简单介绍一下cxf的配置过程,如果更深入的了解,可以通过下载的sample当中的example查看更具体的内容。

    1,导入jar包,这里同样我把所有cxf下的jar包导入到测试项目当中

    2,创建接口文件

      

    import javax.jws.WebParam;
    import javax.jws.WebService;
    import javax.jws.soap.SOAPBinding;
    import javax.jws.soap.SOAPBinding.Style;

    @WebService
    @SOAPBinding(style = Style.RPC)
    public interface IComplexUserService {

    public String getUserByName(@WebParam(name = "name") String name);

    public void setUser(String user);

    }

    3,实现接口文件

      

    import javax.jws.WebParam;
    import javax.jws.WebService;
    import javax.jws.soap.SOAPBinding;
    import javax.jws.soap.SOAPBinding.Style;

    @WebService
    @SOAPBinding(style = Style.RPC)
    @SuppressWarnings("deprecation")
    public class ComplexUserService implements IComplexUserService {

    public String getUserByName(@WebParam(name = "name") String name) {

    return name;
    }

    public void setUser(String user) {

    System.out.println("############Server setUser###########");
    System.out.println("setUser:" + user);
    }

    }

    4,修改Web.xml文件,添加内容如下:

      

    <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <listener><!-- 为防止内存泄漏 -->
    <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
    </listener>
    <servlet>
    <servlet-name>CXFService</servlet-name>
    <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
    </servlet>
    <servlet-mapping>
    <servlet-name>CXFService</servlet-name>
    <url-pattern>/*</url-pattern>
    </servlet-mapping>

    5,修改applicaiontContext.xml文件:

      

    <bean id="userServiceBean" class="com.test.service.ComplexUserService" />
    <jaxws:server id="userService" serviceClass="com.test.service.IComplexUserService"
    address="/Users"> <!-- address 配置为webservice服务地址 -->
    <jaxws:serviceBean>
    <!-- 要暴露的 bean 的引用 -->
    <ref bean="userServiceBean" />
    </jaxws:serviceBean>
    </jaxws:server>

     tip:3.0之后的版本不再需要导入cxf.xml,cxf-extension-soap.xml,cxf-servlet.xml这三个文件。

    6,发布webservice,根据配置的信息访问webservice地址:http://localhost:8090/CXFWJS/Users?wsdl,从浏览器中可以看到如下内容:发布成功

      

    客户端调用:

      使用wsimport将webservice接口文件下载到本地,并在项目当中引用。下载下来的文件如下:

    客户端调用程序如下:

    public static void main(String[] args) throws Exception {

    //第一种调用
    JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
    factory.setServiceClass(IComplexUserService.class);
    factory.setAddress("http://localhost:8090/CXFWJS/Users");
    IComplexUserService service = (IComplexUserService) factory.create();
    System.out.println("#############Client getUserByName##############");
    String user = service.getUserByName("test");
    System.out.println(user);
    service.setUser(user);

    //第二种调用方式
    IComplexUserServiceService iComplexUserServiceService = new IComplexUserServiceService();
    IComplexUserService iComplexUserService = iComplexUserServiceService.getIComplexUserServicePort();
    String result = iComplexUserService.getUserByName("testUser");
    System.out.println(result);

    //第三种调用方式
    JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
    //也可以通过wsdl文件调用
    Client client = dcf.createClient("http://localhost:8090/CXFWJS/Users?wsdl");
    Object[] result2 = client.invoke("getUserByName", "test User");
    System.out.println(result2[0]);
    }

  • 相关阅读:
    邮件收件与草稿箱sql
    [转]全局变量与全局静态变量的区别:
    工作:8月份6-14号的工作初级经验(零碎) 关于传值id经常犯的错误
    网络婚礼之AFNetWorking3.0
    网络热恋之NSURLSession
    网络热恋之json解析
    网络热恋之SDWebImage
    网络热恋之XML解析
    网络&热恋NSURLConnection代理及GET¥POST请求
    网络邂逅&网络异步请求
  • 原文地址:https://www.cnblogs.com/adam1991/p/7645579.html
Copyright © 2020-2023  润新知