Spring+CXF整合来管理webservice(服务器启动发布webservice)
Spring+CXF整合来管理webservice
实现步骤:
1. 添加cxf.jar 包(集成了Spring.jar、servlet.jar ),spring.jar包 ,servlet.jar 包
2. 编写业务类,通过CXF来发布webservice
员工管理:
方法 :添加员工、查询员工
3. 添加一个CXF请求的 Servlet,用来处理webservice的请求
过滤的地址/ws/*
4. 配置Spring的配置文件: applicationContext.xml ,把cxf的bean在spring配置
5. 在web.xml中配置 CXF的 Servlet , 添加spring的监听
6. 通过wsimport生成本地代理 ,访问webservice
1.jar包:
2.业务类
bean
package cn.it.ws.cxf.bean; public class Employee { private Integer id; private String name; private Integer age; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } }
接口
package cn.it.ws.cxf.b; import java.util.List; import javax.jws.WebParam; import javax.jws.WebResult; import javax.jws.WebService; import cn.it.ws.cxf.bean.Employee; @WebService(serviceName="EmployeeService") public interface EmployeeManager { public abstract void add(@WebParam(name="employee")Employee employee); public abstract @WebResult(name="employees")List<Employee> query(); }
实现类:
package cn.it.ws.cxf.b; import java.util.ArrayList; import java.util.List; import cn.it.ws.cxf.bean.Employee; /**员工管理的业务实现类 */ public class EmployeeManagerImpl implements EmployeeManager { private List<Employee> employees=new ArrayList<>(); @Override public void add(Employee employee){ //添加到集合中 employees.add(employee); } @Override public List<Employee> query(){ return employees; } }
3.webxml配置CSFServlet(jar包自带的servlet)
<!-- 添加 CXF 的Servlet ,处理 webservice的请求 --> <servlet> <servlet-name>cxf</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> <load-on-startup>0</load-on-startup> </servlet> <servlet-mapping> <servlet-name>cxf</servlet-name> <url-pattern>/ws/*</url-pattern> </servlet-mapping>
4.配置spring的applicationContext.xml
(1)需要加入CXF约束:
jaxrs.xsd是restful的约束,restful与ws差不多,只是restful接收的是json数据。
记住命名空间路径
applicationContext.xml配置约束
增加约束先引入命名空间,再引入约束位置,因为有schemas目录,所以加上schemas
(2)applicationContext.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:p="http://www.springframework.org/schema/p" xmlns:jaxws="http://cxf.apache.org/jaxws" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> <bean id="employeeManagerImpl" class="cn.it.ws.cxf.b.EmployeeManagerImpl"></bean> <!-- 配置cxf 地址: http://192.168.114.10:8080/CXF_Server/ws/employeeManager 组成 : http://192.168.114.10:8080 +CXF_Server( 项目名)+ws(过滤的路径)+/employeeManager(自定义部分) 服务类 : 服务的实现类: 拦截器 --> <jaxws:server address="/employeeManager" serviceClass="cn.it.ws.cxf.b.EmployeeManager"> <jaxws:serviceBean> <ref bean="employeeManagerImpl"/> </jaxws:serviceBean> <!-- 配置输入显示日志信息的拦截器 --> <jaxws:inInterceptors> <bean class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean> </jaxws:inInterceptors> <jaxws:outInterceptors> <bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"></bean> </jaxws:outInterceptors> </jaxws:server> </beans>
5.web,xml配置spring监听
<?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" 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>CXF_Server</display-name> <!-- 添加 CXF 的Servlet ,处理 webservice的请求 --> <servlet> <servlet-name>cxf</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> <load-on-startup>0</load-on-startup> </servlet> <servlet-mapping> <servlet-name>cxf</servlet-name> <url-pattern>/ws/*</url-pattern> </servlet-mapping> <!-- Spring 监听添加 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> </web-app>
6.启动tomcat进行测试:
7.生成本地代理
C:\Users\liqiang>cd Desktop
C:\Users\liqiang\Desktop>wsimport http://localhost/CXF_server/ws/employeeManager
?wsdl
parsing WSDL...
Generating code...
Compiling code...
打包:
C:\Users\liqiang\Desktop>jar -cvf EmployeeWS.jar ./cn
已添加清单
正在添加: cn/(输入 = 0) (输出 = 0)(存储了 0%)
正在添加: cn/it/(输入 = 0) (输出 = 0)(存储了 0%)
正在添加: cn/it/ws/(输入 = 0) (输出 = 0)(存储了 0%)
正在添加: cn/it/ws/cxf/(输入 = 0) (输出 = 0)(存储了 0%)
正在添加: cn/it/ws/cxf/b/(输入 = 0) (输出 = 0)(存储了 0%)
正在添加: cn/it/ws/cxf/b/Add.class(输入 = 665) (输出 = 383)(压缩了 42%)
正在添加: cn/it/ws/cxf/b/AddResponse.class(输入 = 432) (输出 = 286)(压缩了 33%)
正在添加: cn/it/ws/cxf/b/Employee.class(输入 = 980) (输出 = 495)(压缩了 49%)
正在添加: cn/it/ws/cxf/b/EmployeeManager.class(输入 = 1007) (输出 = 522)(压缩了
48%)
正在添加: cn/it/ws/cxf/b/EmployeeService.class(输入 = 2326) (输出 = 1049)(压缩了
54%)
正在添加: cn/it/ws/cxf/b/ObjectFactory.class(输入 = 2506) (输出 = 902)(压缩了 64
%)
正在添加: cn/it/ws/cxf/b/package-info.class(输入 = 242) (输出 = 198)(压缩了 18%)
正在添加: cn/it/ws/cxf/b/Query.class(输入 = 414) (输出 = 281)(压缩了 32%)
正在添加: cn/it/ws/cxf/b/QueryResponse.class(输入 = 789) (输出 = 465)(压缩了 41%
)
测试类:
import cn.it.ws.cxf.b.Employee; import cn.it.ws.cxf.b.EmployeeManager; import cn.it.ws.cxf.b.EmployeeService; public class _Main2 { public static void main(String[] args) { EmployeeService em = new EmployeeService(); EmployeeManager employeeManager = em.getEmployeeManagerPort(); Employee employee = new Employee(); employee.setAge(25); employeeManager.add(employee); System.out.println(employeeManager.query().get(0).getAge()); } }
客户端控制台:
25
服务端控制台:
九月 22, 2017 6:58:35 下午 org.apache.cxf.interceptor.AbstractLoggingInterceptor log 信息: Inbound Message ---------------------------- ID: 12 Address: http://localhost/CXF_server/ws/employeeManager?wsdl Http-Method: GET Content-Type: Headers: {Accept=[text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2], connection=[keep-alive], Content-Type=[null], host=[localhost], user-agent=[Java/1.7.0_80]} -------------------------------------- 九月 22, 2017 6:58:36 下午 org.apache.cxf.interceptor.AbstractLoggingInterceptor log 信息: Inbound Message ---------------------------- ID: 13 Address: http://localhost/CXF_server/ws/employeeManager Encoding: UTF-8 Http-Method: POST Content-Type: text/xml; charset=utf-8 Headers: {Accept=[text/xml, multipart/related], connection=[keep-alive], Content-Length=[204], content-type=[text/xml; charset=utf-8], host=[localhost], SOAPAction=[""], user-agent=[JAX-WS RI 2.2.4-b01]} Payload: <?xml version="1.0" ?><S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"><S:Body><ns2:add xmlns:ns2="http://b.cxf.ws.it.cn/"><employee><age>25</age></employee></ns2:add></S:Body></S:Envelope> -------------------------------------- 九月 22, 2017 6:58:36 下午 org.apache.cxf.interceptor.AbstractLoggingInterceptor log 信息: Outbound Message --------------------------- ID: 13 Encoding: UTF-8 Content-Type: text/xml Headers: {} Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:addResponse xmlns:ns2="http://b.cxf.ws.it.cn/"/></soap:Body></soap:Envelope> -------------------------------------- 九月 22, 2017 6:58:36 下午 org.apache.cxf.interceptor.AbstractLoggingInterceptor log 信息: Inbound Message ---------------------------- ID: 14 Address: http://localhost/CXF_server/ws/employeeManager Encoding: UTF-8 Http-Method: POST Content-Type: text/xml; charset=utf-8 Headers: {Accept=[text/xml, multipart/related], connection=[keep-alive], Content-Length=[163], content-type=[text/xml; charset=utf-8], host=[localhost], SOAPAction=[""], user-agent=[JAX-WS RI 2.2.4-b01]} Payload: <?xml version="1.0" ?><S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"><S:Body><ns2:query xmlns:ns2="http://b.cxf.ws.it.cn/"/></S:Body></S:Envelope> -------------------------------------- 九月 22, 2017 6:58:36 下午 org.apache.cxf.interceptor.AbstractLoggingInterceptor log 信息: Outbound Message --------------------------- ID: 14 Encoding: UTF-8 Content-Type: text/xml Headers: {} Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:queryResponse xmlns:ns2="http://b.cxf.ws.it.cn/"><employees><age>25</age></employees><employees><age>25</age></employees><employees><age>25</age></employees></ns2:queryResponse></soap:Body></soap:Envelope> --------------------------------------
切换console: