导入maven依赖
<!--webService配置-->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxrs</artifactId>
<version>3.0.5</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http-jetty</artifactId>
<version>3.0.5</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.6</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.1.1</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.1.1</version>
</dependency>
1、soap接口
(1)spring配置application-webservice.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"
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"
default-autowire="byName">
<jaxws:endpoint id="wsServiceSoap" implementor="com.webservice.impl.WsServiceSoapImpl"
address="/wsServiceSoap">
</jaxws:endpoint>
</beans>
(2)在web.xml里面配置webservice
<!--webService在web.xml的配置-->
<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>/ws/*</url-pattern>
</servlet-mapping>
(3)编写WsServiceSoap接口
@WebService(targetNamespace = "com.webservice")
public interface WsServiceSoap {
public String syncUser(@WebParam(name = "userId") String userId);
}
(4)编写WsServiceSoap接口实现类
@Component("wsServiceSoap")
@WebService(endpointInterface = "com.webservice.WsServiceSoap"
, targetNamespace = "com.webservice")
public class WsServiceSoapImpl implements WsServiceSoap {
public String syncUser(@WebParam(name = "userId") String userId) {
//业务代码
System.out.println(userId);
return "success";
}
}
运行后在浏览器访问http:ip:port/ws/wsServiceSoap?wsdl即可看到接口信息,此时webservice的soap接口可以正常访问了,可以通过soapUI软件访问wsdl接口
2、restful接口
(1)spring配置application-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: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/jaxrs
http://cxf.apache.org/schemas/jaxrs.xsd">
<!--webService配置-->
<jaxrs:server address="/synApi/syncUser">
<jaxrs:serviceBeans>
<bean class="com.webservice.impl.WsServiceImpl"></bean>
</jaxrs:serviceBeans>
</jaxrs:server>
</beans>
(2)在web.xml里面配置webservice
<!--webService在web.xml的配置-->
<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>/ws/*</url-pattern>
</servlet-mapping>
(3)编写WsService接口
@WebService
public interface WsService {
@WebMethod
public String syncUser();
}
(4)编写WsService接口的实现类
@Component("wsServiceImpl")
@WebService(endpointInterface = "com.webservice.WsService")
public class WsServiceImpl implements WsService {
//这里通过这个对象接收数据,可根据实际情况接收数据
private WsUserTpm wsUserTpm;
@Override
@Consumes(MediaType.APPLICATION_JSON) //接收json的数据
@Produces(MediaType.APPLICATION_JSON) //返回json格式的数据
@POST //提交方式
public String syncUser() {
System.out.println(wsUserTpm);
return "success";
}
}
此时可以用postman工具访问接口http:ip:port/ws/synApi/syncUser进行测试