停更一个多月的我又回来了。
今天随笔记一下CXF配合Spring的使用,方便今后用的时候忘带笔记,随时上网就可以看到。
CXF技术:Apache CXF 是一个开放源代码框架,提供了用于方便地构建和开发 Web 服务的可靠基础架构。它允许创建高性能和可扩展的服务,您可以将这样的服务部署在 Tomcat 和基于 Spring 的轻量级容器中,以及部署在更高级的服务器上。
简单来说,cxf技术就是在不同服务器之间或者不同语言之间可以传输 数据的一种框架,用到的地方就是web service协议下。
首先是jar包:CXF引入的jar包主要有这几个:一个是cxf-core-3.1.4.jar,是核心jar包,可以在Apache官网上下载;之后还有cxf-rt-bindings-soap-3.1.4.jar,cxf-rt-databinding-jaxb-3.1.4.jar,cxf-rt-frontend-jaxws-3.1.4.jar,cxf-rt-frontend-simple-3.1.4.jar,cxf-rt-transports-http-3.1.4.jar,cxf-rt-ws-addr-3.1.4.jar,cxf-rt-ws-policy-3.1.4.jar,cxf-rt-wsdl-3.1.4.jar;当然Spring里的jar包和依赖不可少。
首先,写服务端接口,注明@WebService。
然后编写实现类和实现方法:使用注解
@WebService(endpointInterface = “web service interface location”, serviceName = “your name”)
@Component(“webservice”)
服务端就这么轻松地完成了。我这里是将一个实体对象进行保存,因此还需要在实现类里对数据进行反序列化等操作,并调用service层存储对象。(这里如果web service没有必要的逻辑判断情况下,不建议调用acton层进行处理,直接调用service层)
之后,开始配置文件的编写:首先是web.xml文件:
<!-- Web Service -->
<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>/service/*</url-pattern>
</servlet-mapping>
这里注意url-pattern就是调用web service的路径地址,后面还会用到,其他就是固定格式。
再配置Spring:创建一个webservice.xml:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:cache="http://www.springframework.org/schema/cache" xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd
http://cxf.apache.org/core
http://cxf.apache.org/schemas/core.xsd"
default-autowire="byName">
<!-- webServcie -->
<import resource="classpath:META-INF/cxf/cxf.xml" />
<jaxws:endpoint id="your ID" address="/position">
<jaxws:implementor>
<ref bean="webservice" />
</jaxws:implementor>
</jaxws:endpoint>
<!-- webServcie End -->
</beans>
之后用一个Spring的xml文件引入改配置文件:
<import resource="classpath:webservice.xml position" />
<context:component-scan base-package="webservice interface position" />
运行tomcat,运行http://localhost/project/service/position?wsdl就可以看到开启的web service了。
客户端配置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:jaxrs="http://cxf.apache.org/jaxrs" xmlns:cxf="http://cxf.apache.org/core"
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
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd">
-->
<jaxws:client id="WSID" address="http://localhost/project/service/position"
serviceClass="webservice interface position">
</jaxws:client>
</beans>
调用服务端的方法