本文主要涉及的webservice采用SOAP协议、采用JAX-WS作为前端、JAXB数据绑定。需要加入以下依赖:
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-bindings-soap</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-databinding-jaxb</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>2.7.0</version>
</dependency>
1、server端
与之前基于JAX-WS的webservice一致,作为前端并不需要有任何修改。在这个例子中包含了传递String、自定义对象、Map等
@WebService(serviceName = "cxfServer")
public class CXFServiceImpl implements CXFService {
@Override
@WebMethod
public String sayHello(String name) {
return "Hello, " + name + ", from CXF";
}
@Override
@WebMethod
@WebResult
@XmlJavaTypeAdapter(value = MapAdapter.class)
public Map<String, Person> queryPerson() {
Map<String, Person> map = new HashMap<String, Person>();
map.put("p1", new Person("robin", "robin_123"));
map.put("p2", new Person("ace", "ace_456"));
return map;
}
@Override
@WebMethod
public void savePerson(@WebParam List<Person> list) {
if (list != null && !list.isEmpty()) {
for (Person person : list) {
System.out.println(ToStringBuilder.reflectionToString(person, ToStringStyle.SHORT_PREFIX_STYLE));
}
}
}
}
注意对传递Map,在JAX-WS是不能直接传递,需要作为一个包装类传递。而在CXF中则可以直接传递,这里还是采用JAXB作为解析对象故这里的MapAdapter与之前的一致.见这里
2、发布为一个webservice
1、jaxwsserver
JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();
factory.setServiceClass(CXFServiceImpl.class);
factory.setAddress("http://localhost:8080/service/helloCxfService");
//optional: set in interceptors
factory.getInInterceptors().add(new LoggingInInterceptor());
//optional: set out interceptors
factory.getOutInterceptors().add(new LoggingOutInterceptor());
factory.create();
由于采用jetty作为默认的Server的实现,需要加入
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http-jetty</artifactId>
<version>2.7.0</version>
</dependency>
运行后即可通过http://localhost:8080/service/helloCxfService?wsdl查看发布结果。或者可以通过这样测试
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.setServiceClass(CXFService.class);
factory.setAddress("http://localhost:8080/service/helloCxfService");
CXFService server = (CXFService) factory.create();
2、spring集成的几种方式
需要在spring的配置文件加入相应的schema及location:
xmlns:jaxws="http://cxf.apache.org/jaxws"
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
打开位于cxf-rt-frontend-jaxws-2.7.0.jar/schemas/jaxws.xsd文件,我们查看定义了三种方式:
jaxws:endpoint
jaxws:server
jaxws:client
前两者用来申明发布server,后者作为客户端的配置。下面来看看与spring继承的几种方式:
1、采用jaxws:server配置
<!-- 采用jaxws:server方式 -->
<jaxws:server id="helloCxf" serviceClass="org.ws.server.cxf.chap1.CXFService"
address="http://localhost:8080/service/helloCxf">
<jaxws:serviceBean>
<bean class="org.ws.server.cxf.chap1.impl.CXFServiceImpl" />
</jaxws:serviceBean>
<jaxws:inInterceptors>
<bean class="org.apache.cxf.interceptor.LoggingInInterceptor" />
</jaxws:inInterceptors>
<jaxws:outInterceptors>
<bean class="org.apache.cxf.interceptor.LoggingOutInterceptor" />
</jaxws:outInterceptors>
</jaxws:server>
这里jaxws:endpoint的配置大同小异,具体参数配置见jaxws.xsd
2、由于采用JAX-WS作为前端,作为对JaxWsServerFactoryBean实例的注入:
<!-- 作为对JaxWsServerFactoryBean的注入 -->
<bean id="cxfServiceFactory" class="org.apache.cxf.jaxws.JaxWsServerFactoryBean">
<property name="serviceClass" value="org.ws.server.cxf.chap1.impl.CXFServiceImpl" />
<property name="address" value="http://localhost:8080/service/helloCxf" />
<property name="inInterceptors">
<list>
<bean class="org.apache.cxf.interceptor.LoggingInInterceptor" />
</list>
</property>
<property name="outInterceptors">
<list>
<bean class="org.apache.cxf.interceptor.LoggingOutInterceptor" />
</list>
</property>
</bean>
<bean id="helloCxfService" class="org.ws.server.cxf.chap1.impl.CXFServiceImpl"
factory-bean="cxfServiceFactory" factory-method="create" />
针对上面两种配置,可以通过在http://localhost:8080/service/helloCxf?wsdl访问发布成功与否。当然上面的address采用了绝对路径,实际上和web项目的整合只需要指定到相对路径即可。
3、与web项目(tomcat)
还是采用上面第二种配置,这里只需要修改address的路径如:
<property name="address" value="/helloCxf" />
在web.xml中需要添加一个servlet对请求的拦截处理,如:
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/service/*</url-pattern>
</servlet-mapping>
运行后即可通过容器发布的路径访问,如:http://localhost:8080/webservice/service/helloCxf?wsdl
3、客户端访问
以上都是讲述的服务端的实现,如何发布一个服务。在上面的各种服务都是基于SOAP协议,用户可以获得生产的wsdl文件,来生产相应的客户端代码来完成远程的调用。在前面介绍JAX-WS的时候讲解了两种工具来生产客户端代码:wsimport/wsgen。而CXF也同样提供了类似的一些列工具,这里正对这种场景我们需要用到的是wsdl2java,位于CXF_HOME/bin目录
可以通过wsdl2java -help来看具体的参数,这里生产一个客户端代码如下:
wsdl2java -p org.sample.ws.client.cxf.chap1 -keep -verbose http://localhost:8080/webservice/service/helloCxf?wsdl
这样在指定的包下就生产了客户端代码。下面通过spring配置类测试客户端的调用,与上面发布服务一样,通过两种方式:
<jaxws:client id="helloCxfClient"
address="http://localhost:8080/webservice/service/helloCxf"
serviceClass="org.sample.ws.client.cxf.chap1.CXFService">
<jaxws:inInterceptors>
<bean class="org.apache.cxf.interceptor.LoggingInInterceptor" />
</jaxws:inInterceptors>
<jaxws:outInterceptors>
<bean class="org.apache.cxf.interceptor.LoggingOutInterceptor" />
</jaxws:outInterceptors>
</jaxws:client>
这里需要注意哪些配置是作为element哪些是attribute
2、作为对JaxWsProxyFactoryBean注入的实现:
<!-- 作为对JaxWsProxyFactoryBean的注入 -->
<bean id="cxfClientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
<property name="serviceClass" value="org.sample.ws.client.cxf.chap1.CXFService" />
<property name="address" value="http://localhost:8080/webservice/service/helloCxf" />
<property name="inInterceptors">
<list>
<bean class="org.apache.cxf.interceptor.LoggingInInterceptor" />
</list>
</property>
<property name="outInterceptors">
<list>
<bean class="org.apache.cxf.interceptor.LoggingOutInterceptor" />
</list>
</property>
</bean>
<bean id="helloCxfService" class="org.sample.ws.client.cxf.chap1.CXFService"
factory-bean="cxfClientFactory" factory-method="create" />
客户端测试代码:
ClassPathXmlApplicationContext xmlApplication = new ClassPathXmlApplicationContext("cxf-spring.xml");
CXFService service = (CXFService) xmlApplication.getBean("helloCxfService");
System.out.println(service.sayHello("robin"));
4、异步
由于采用JAX-WS作为前端,针对之前在用JAX-WS发布异步服务,我们这里也可以采用相同的方式:
编写binding.xml文件,指定异步方式:
<bindings
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
wsdlLocation="http://localhost:8080/webservice/service/helloCxf?wsdl"
xmlns="http://java.sun.com/xml/ns/jaxws">
<bindings node="wsdl:definitions">
<enableAsyncMapping>true</enableAsyncMapping>
</bindings>
</bindings>
在生成客户端的时候需要指定该binding.xml文件
wsdl2java -p org.sample.ws.client.cxf.chap2 -b .\binding.xml -keep -verbose http://localhost:8080/webservice/service/helloCxf?wsdl
测试:
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.setServiceClass(CXFService.class);
factory.setAddress("http://localhost:8080/webservice/service/helloCxf");
CXFService server = (CXFService) factory.create();
System.out.println("同步--> server.sayHello(\"robin\"): " + server.sayHello("robin"));
Response<SayHelloResponse> response = server.sayHelloAsync("robin");
while (!response.isDone()) {
System.out.println("异步调用......");
}
System.out.println("异步: " + response.get().getReturn());
分享到:
相关推荐
亲测可用,Jax-ws所需要的JAR包,拷贝到tomcat安装路径的lib里,实现了webservice发布到tomcat,赞!
基于JAX-WS标准采用CXF引擎设计开发WebService
jax-ws方式的webservice服务,基于xml的webservice开发,资料很全面很珍贵,大师所写的书籍,真心不贵
JAX-WS + Spring 实现webService示例
本课程主要针对主流的SOA核心思想、融合WebService、JAX-WS、SCA、 Tuscany及开源产品技术CXF完成了从思想到技术,从技术到项目的课程体系.本课程主要是针对复杂的SOA 思想,通过项目开发的形式融会贯通给学员进行...
EndPoint、JAX-WS方式的WebService,请根据“创建WebService的几种方式简介(EndPoint、JAX-WS、CXF、axis2、自定义Servlet+Document解析)”文章配套练习
一个关于CXF实现jax-ws规范的webservice #CXF框架 Apache CXF=Celtix+Xfire.是一个开源的一个webservice,可以与spring无缝集成。支持soap1.1、1.2、RESTtful或者CORBA。 ##使用CXF实现jax-ws规范的webservice ...
覆盖以下内容:基于JAX-WS规范和CXF自身的前端模式实现,CXF支持的数据绑定(DataBindings),CXF支持的WSDL绑定,CXF支持的传输协议绑定。CXF的调式、配置、日志、发布和工具。CXF实现RESTful服务。CXF对WS-* 的...
2. Frontends:CXF 支持多种“Frontend”编程模型,CXF 实现了 JAX-WS API (遵循 JAX-WS 2.0 TCK 版本),它也包含一个“simple frontend”允许客户端和 EndPoint 的创建,而不需要 Annotation 注解。CXF 既支持 ...
春天 使用 Apache CXF 和 Spring Framework 的 WebService(JAX-WS 和 JAX-RS) Apache CXF: : Spring 框架: : 我想分享有关使用 Spring Framework 的 Web 服务的更多信息。
Frontends:CXF 支持多种“Frontend”编程模型,CXF 实现了 JAX-WS API (遵循 JAX-WS 2.0 TCK 版本),它也包含一个“simple frontend”允许客户端和 EndPoint 的创建,而不需要 Annotation 注解。CXF 既支持 WSDL ...
CXF-webService实例(eclipse工程,有jar包,可运行) jax-ws和pojo两种方式发布
Apache CXF是一个开源的Service框架,它实现了JCP与Web Service中...CXF同样支持多种model 如:JAX-WS,JBI,SCA和CORBA service。CXF设计成可灵活部署到各种容器中包括Spring-based,JBI,SCA, Servlet和J2EE容器。
从流行的汲取灵感,继承了一个完全更改的示例WebService-Definition作为WSDL 展示如何在构建时使用JAX-WS Commons Maven插件从WSDL生成JAXB类-只需运行 mvn clean generate-sources 首先使用SpringBoot,CXF和...
CXF helps you build and develop services using frontend programming APIs, like JAX-WS and JAX-RS. These services can speak a variety of protocols such as SOAP, XML/HTTP, RESTful HTTP, or CORBA and ...
使用java的ssm框架+shiro管理权限+webservice开放接口,主要内容是员工界面的增删改查和ajax实现员工信息修改的实时检验,前端使用bootstrap,还使用了cxf包和jax-ws开发了员工增查的webservice接口(员工是复杂类型).
CXF 继承了 Celtix 和 XFire 两大开源项目的精华,提供了对 JAX-WS 全面的支持,并且提供了多种 Binding 、DataBinding、Transport 以及各种 Format 的支持,并且可以根据实际项目的需要,采用代码优先(Code First...
CXF 继承了 Celtix 和 XFire 两大开源项目的精华,提供了对 JAX-WS 全面的支持,并且提供了多种 Binding 、DataBinding、Transport 以及各种 Format 的支持,并且可以根据实际项目的需要,采用代码优先(Code First...
CXF 继承了 Celtix 和 XFire 两大开源项目的精华,提供了对 JAX-WS 全面的支持,并且提供了多种 Binding 、DataBinding、Transport 以及各种 Format 的支持,并且可以根据实际项目的需要,采用代码优先(Code First...
CXF 继承了 Celtix 和 XFire 两大开源项目的精华,提供了对 JAX-WS 全面的支持,并且提供了多种 Binding 、DataBinding、Transport 以及各种 Format 的支持,并且可以根据实际项目的需要,采用代码优先(Code First...