当前位置:文档之家› 通过CXF开发webService接口(soap协议、maven项目)

通过CXF开发webService接口(soap协议、maven项目)

1. 引入cxf的jar包pom文件里面直接增加依赖< dependency><groupId > junit</ groupId><artifactId > junit</ artifactId><version >4.11 </version ><scope >test </scope ></dependency ><dependency ><groupId >org.springframework </groupId ><artifactId >spring- webmvc</ artifactId><version >4.0.0.RELEASE </version ></dependency ><dependency ><groupId >org.apache.cxf </groupId ><artifactId > apache-cxf </artifactId ><version >2.4.3</version ><type > pom</ type></dependency >2. 配置web.xml文件<? xml version= "1.0" encoding= "UTF-8" ?>< web-app xmlns= "/xml/ns/javaee"xmlns:xsi= "/2001/XMLSchema-instance" xsi:schemaLocation="/xml/ns/javaee/xml/ns/javaee/web-app_3_1.xsd" version= "3.1" >< display-name >Archetype Created Web Application</display-name >< context-param ><param-name >contextConfigLocation</param-name ><param-value >classpath:config/spring/metadata WebService-spring.xml </param-value ></context-param ><listener ><listener-class >org.springframework.web.conte xt.ContextLoaderListener </listener-class ></listener ><servlet ><servlet-name >CXFServlet </servlet-name ><servlet-class >org.apache.cxf.transport.servl et.CXFServlet </servlet-class ></servlet ><servlet-mapping ><servlet-name >CXFServlet </servlet-name ><url-pattern >/services/* </url-pattern ></servlet-mapping ><filter ><filter-name >encodingFilter </filter-name >< filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class ><init-param ><param-name >encoding </param-name ><param-value >UTF-8 </param-value ></init-param ><init-param ><param-name >forceEncoding </param-name ><param-value >true </param-value ></init-param ></filter ><filter-mapping ><filter-name >encodingFilter </filter-name ><url-pattern >/* </url-pattern ></filter-mapping ></ web-app>3. 配置cxf.xml文件<? xml version= "1.0" encoding= "UTF-8" ?>< beans xmlns="/schema/beans"xmlns:xsi="/2001/XMLSchema-instance" xmlns:jaxws= "/jaxws"xmlns:context="/schema/context"xmlns:jaxrs= "/jaxrs"xsi:schemaLocation="/schema/beans/schema/beans/spring-beans.xsd/jaxws/schemas/jaxws.xsd/schema/context/schema/context/spr ing-context.xsd/jaxrs/schemas/jaxrs.xsd" ><!-- simpleWebService.xml配置文件 start --><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" /><context:annotation-config /><!-- SOAP 服务开放 --><bean id = "wSS4JInInterceptor" class="org.apache.cxf.ws.security.wss4j.WSS4JInInterceptor" ><constructor-arg ><!--<map ><entry key = "action" value= "UsernameToken" /><entry key = "passwordType" value= "PasswordText" /><entry key = "user" value= "cxfServer" /><entry key = "passwordCallbackRef" value= "ss" /></map >--></constructor-arg ></bean ><bean id = "jaxWsServerFactoryBean" class="org.apache.cxf.jaxws.JaxWsServerFactoryBean"scope= "prototype" ><property name = "inInterceptors"><list ><!-- <ref bean = "wSS4JInInterceptor" /> --></list ></property ></bean ><bean id = "jaxWsWebServicePublisherBeanPostProcessor"class="org.apache.cxf.jaxws.spring.JaxWsWebServicePublisherBean PostProcessor" ><property name = "urlPrefix" value= "/" /><property name = "prototypeServerFactoryBeanName" value= "jaxWsServerFactoryBean" /></bean ><!-- simpleWebService.xml配置文件 end--><!-- 导入soap协议的服务支持 serviceClass为对应的webService接口类-->< jaxws:server id= "ResumeUpload" serviceClass= "com.sigmatrix.soap.webService.DemoService"address= "/ResumeUpload" ><!-- 添加webService接口实现类 --><jaxws:serviceBean ><ref bean = "demoServiceImpl" /></jaxws:serviceBean ><!-- 添加协议使用MTOM附件--><jaxws:properties ><entry key = "mtom-enabled" value= "true" /></jaxws:properties ></jaxws:server ></ beans>4. 要开放的接口加上cxf服务的注解接口类:@WebService (portName ="inbound.webServices.ticket.saServiceSoap12" )@javax.xml.ws.soap. MTOMpublic interface DemoService {@WebMethodpublic String demo( @WebParam(name = "demo") Demo demo);}接口实现类:@Component@WebService (portName ="inbound.webServices.ticket.saServiceSoap12" )@BindingType (value ="/2003/05/soap/bindings/HTTP/" )public class DemoServiceImpl implements DemoService { public String Demo(Demo demo) {String userName = demo.getUserName();String password= demo.getPassword();return "success";}Demo实体类:public Class Demo() {public String userName;public String password;public String getUserName() {return userName;}public void setUserName(String userName) {erName = userName;}public String getPassword() {return password;}public void setPassword(String password) {this.password= password;}}5. 部署到tomcat启动即可,测试访问 ip:端口号//项目名称/services/。

相关主题