webservice之CXF注解实现(一)
2013-10-24 16:20
381 查看
1.关于CXF(摘自百度百科)
Apache CXF = Celtix + XFire,开始叫 Apache CeltiXfire,后来更名为 Apache CXF 了,以下简称为 CXF。CXF 继承了 Celtix 和 XFire 两大开源项目的精华,提供了对 JAX-WS 全面的支持,并且提供了多种 Binding 、DataBinding、Transport 以及各种 Format 的支持,并且可以根据实际项目的需要,采用代码优先(Code First)或者 WSDL 优先(WSDL First)来轻松地实现 Web Services
的发布和使用。Apache CXF已经是一个正式的Apache顶级项目。
Apache CXF 是一个开源的 Services 框架,CXF 帮助您利用 Frontend 编程 API 来构建和开发 Services ,像 JAX-WS 。这些 Services 可以支持多种协议,比如:SOAP、XML/HTTP、RESTful HTTP 或者 CORBA ,并且可以在多种传输协议上运行,比如:HTTP、JMS 或者 JBI,CXF 大大简化了 Services 的创建,同时它继承了 XFire 传统,一样可以天然地和 Spring 进行无缝集成。
2.下载
http://cxf.apache.org/download.html
3.集成到eclipse
笔者下载的版本是2.7.7,进入apache-cxf-2.7.7/lib目录,将所需jar包复制到工程中,需要的jar包较多,如图:
![](http://img.blog.csdn.net/20131024163046890?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbG9vazg1OTI3/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
4.web.xml
5.cxf-servlet.xml
6.IService.java
网上的例子多是String类型,这里使用了自定义的Java类型,RequestCondition类的代码这里不贴出了,读者可自行建立该bean,添加属性age并添加相应的set、get方法
7.IServiceImpl.java
8.部署并测试
将项目部署到web服务器,如tomcat
编写测试类
Apache CXF = Celtix + XFire,开始叫 Apache CeltiXfire,后来更名为 Apache CXF 了,以下简称为 CXF。CXF 继承了 Celtix 和 XFire 两大开源项目的精华,提供了对 JAX-WS 全面的支持,并且提供了多种 Binding 、DataBinding、Transport 以及各种 Format 的支持,并且可以根据实际项目的需要,采用代码优先(Code First)或者 WSDL 优先(WSDL First)来轻松地实现 Web Services
的发布和使用。Apache CXF已经是一个正式的Apache顶级项目。
Apache CXF 是一个开源的 Services 框架,CXF 帮助您利用 Frontend 编程 API 来构建和开发 Services ,像 JAX-WS 。这些 Services 可以支持多种协议,比如:SOAP、XML/HTTP、RESTful HTTP 或者 CORBA ,并且可以在多种传输协议上运行,比如:HTTP、JMS 或者 JBI,CXF 大大简化了 Services 的创建,同时它继承了 XFire 传统,一样可以天然地和 Spring 进行无缝集成。
2.下载
http://cxf.apache.org/download.html
3.集成到eclipse
笔者下载的版本是2.7.7,进入apache-cxf-2.7.7/lib目录,将所需jar包复制到工程中,需要的jar包较多,如图:
4.web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <display-name>PortalService</display-name> <!-- cfx webSerivice --> <servlet> <servlet-name>cxf</servlet-name> <servlet-class> org.apache.cxf.transport.servlet.CXFServlet </servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>cxf</servlet-name> <url-pattern>/services/*</url-pattern> </servlet-mapping> <session-config> <session-timeout>60</session-timeout> </session-config> </web-app>
5.cxf-servlet.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:soap="http://cxf.apache.org/bindings/soap" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/bindings/soap http://cxf.apache.org/schemas/configuration/soap.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> <!-- ;服务接口 --> <jaxws:server id="jaxwsService" serviceClass="com.look.service.IService" address="/test"> <!-- address为服务发布二级地址 完整地址为 /项目发布名称/cfx拦截地址/address (cfx拦截地址在web.xml中url-pattern标签中配置) --> <jaxws:serviceBean> <!--服务实现类 --> <bean class="com.look.service.IServiceImpl" /> </jaxws:serviceBean> </jaxws:server> </beans>
6.IService.java
package com.look.service; import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebService; import com.protal.model.RequestCondition; @WebService public interface IService { @WebMethod String test(@WebParam RequestCondition requestCondition); }
网上的例子多是String类型,这里使用了自定义的Java类型,RequestCondition类的代码这里不贴出了,读者可自行建立该bean,添加属性age并添加相应的set、get方法
7.IServiceImpl.java
package com.look.service; import com.protal.model.RequestCondition; public class IServiceImpl implements IService { @Override public String test(RequestCondition requestCondition) { return “年龄是:”+requestCondition.getAge(); } }
8.部署并测试
将项目部署到web服务器,如tomcat
编写测试类
package com.look.utest; import org.apache.cxf.interceptor.LoggingInInterceptor; import org.apache.cxf.interceptor.LoggingOutInterceptor; import org.apache.cxf.jaxws.JaxWsProxyFactoryBean; import com.look.service.IService; import com.protal.model.RequestCondition; public class Test { public static void main(String[] args) { JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean(); factory.getInInterceptors().add(new LoggingInInterceptor()); factory.getOutInterceptors().add(new LoggingOutInterceptor()); factory.setServiceClass(IService.class); factory.setAddress("http://localhost:8080/PortalService/services/test"); IService client = (IService) factory.create(); RequestCondition requestCondition = new RequestCondition(); requestCondition.setAge("18"); String msg = client.test(requestCondition); System.out.println(msg); } }
--------------------------测试结果----------------------------
十月 24, 2013 4:16:10 下午 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass 信息: Creating Service {http://service.look.com/}IServiceService from class com.look.service.IService 十月 24, 2013 4:16:10 下午 org.apache.cxf.services.IServiceService.IServicePort.IService 信息: Outbound Message --------------------------- ID: 1 Address: http://localhost:8080/PortalService/services/test Encoding: UTF-8 Http-Method: POST Content-Type: text/xml Headers: {Accept=[*/*], SOAPAction=[""]} Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:test xmlns:ns2="http://service.look.com/"><arg0><age>18</age></arg0></ns2:test></soap:Body></soap:Envelope> -------------------------------------- 十月 24, 2013 4:16:10 下午 org.apache.cxf.services.IServiceService.IServicePort.IService 信息: Inbound Message ---------------------------- ID: 1 Response-Code: 200 Encoding: UTF-8 Content-Type: text/xml;charset=UTF-8 Headers: {Content-Length=[214], content-type=[text/xml;charset=UTF-8], Date=[Thu, 24 Oct 2013 08:16:10 GMT], Server=[Apache-Coyote/1.1]} Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:testResponse xmlns:ns2="http://service.look.com/"><return>年龄是:18</return></ns2:testResponse></soap:Body></soap:Envelope> -------------------------------------- 年龄是:18
相关文章推荐
- Tomcat下通过CXF实现不用注解发布WebService
- WebService-CXF和spring整合实现webservice案例(Spring配置注解环境)
- WebService之CXF注解之三(Service接口实现类)
- WebService之CXF注解之三(Service接口实现类)
- WebService之CXF注解之三(Service接口实现类)
- webservice之CXF注解实现(二)
- cxf webservice 注解方式实现!!!!!
- Tomcat下通过CXF实现不用注解发布WebService
- webservice之CXF注解实现(三)
- CXF实现简单webservice应用、集成spring发布到tomcat
- 利用cxf实现webservice完整demo
- spring boot-实现WebService(CXF实现)的拦截器(Interceptor)
- java cxf实现webservice接口方式之不依赖spring
- 使用Spring+CXF开发WebService,使用注解方式
- WebService之CXF注解报错(二)
- WebService之CXF注解之一(封装类)
- CXF+Spring 实现WebService
- 现有项目整合CXF实现webservice对接
- WebService -- Java 实现之 CXF (WebService 服务器端接口)
- SOAP方式的CXF WebService实现