通过CXF,开发soap协议接口

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= "http://xmlns.jcp.org/xml/ns/javaee"

xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation= "http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/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/metadataWebService-spring.xml </param-value >
</context-param >
<listener >
<listener-class >org.springframework.web.context.ContextLoaderListener </listener-class >
</listener >
<servlet >
<servlet-name >CXFServlet </servlet-name >
<servlet-class >org.apache.cxf.transport.servlet.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= "http://www.springframework.org/schema/beans"
xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws= "http://cxf.apache.org/jaxws"
xmlns:context= "http://www.springframework.org/schema/context"
xmlns:jaxrs= "http://cxf.apache.org/jaxrs"
xsi:schemaLocation= "http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://cxf.apache.org/jaxrs
http://cxf.apache.org/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.JaxWsWebServicePublisherBeanPostProcessor" >
<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. MTOM
public interface DemoService {
@WebMethod
public String demo( @WebParam(name = "demo") Demo demo);
}
接口实现类:
@Component
@WebService (portName = "inbound.webServices.ticket.saServiceSoap12" )
@BindingType (value = "http://www.w3.org/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) {
this.userName = userName;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password= password;
}
}
5. 部署到tomcat启动即可,测试访问 ip:端口号//项目名称/services/

posted @ 2018-03-06 10:36  小小小小小小鱼  阅读(1047)  评论(0编辑  收藏  举报