最近我们的系统需要和一个第三方系统对接,对接的方式是通过web service,所以就学习了一下这方面的东西
用CXF来做web service是比较简单的,本文就简单介绍一下如何一步步发布web service,以及调用现有的web service。另外如果系统已经使用了Spring MVC,那么引入CXF需要额外的步骤,见本人另外一篇博客http://kyfxbl.iteye.com/blog/1432920。如果展现层没有用spring mvc,而是用struts2之类的,可以省去这一步
首先介绍怎么发布web service:
第1步是创建一个普通的java接口,然后用@WebService注解声明这是一个web service
-
@H_404_23@
package com.huawei.framework.webservice;
@H_404_23@@H_404_23@
import javax.jws.WebService;
@H_404_23@@H_404_23@
import com.huawei.framework.model.User;
@H_404_23@@H_404_23@
@WebService
@H_404_23@publicinterface HelloWorld {
@H_404_23@@H_404_23@
String sayHi(User user);
@H_404_23@@H_404_23@
}
可以看到,这个接口非常普通,不需要继承什么额外的接口,只需要注解一个@WebService就可以
第2步当然是需要给这个接口提供一个实现类
-
@H_404_23@
package com.huawei.framework.webservice;
@H_404_23@@H_404_23@
import com.huawei.framework.model.User;
@H_404_23@@H_404_23@
publicclass HelloWorldImpl implements HelloWorld {
@H_404_23@@H_404_23@
public String sayHi(User theUser) {
@H_404_23@return"Hello " + theUser.getName() + ",your age is "
@H_404_23@+ theUser.getAge();
@H_404_23@}
@H_404_23@@H_404_23@
}
这个类更加简单,连注解都省了,但是如果这个类实现了不止一个接口,那么就需要加上@WebService注解,不过一般不会这样
第3步就到了cxf出场的时候了,需要在spring配置文件中加上cxf的配置
-
@H_404_23@
<?xmlversion="1.0"encoding="UTF-8"?>
@H_404_23@@H_404_23@
<beansxmlns="http://www.springframework.org/schema/beans"
@H_404_23@xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:jaxws="http://cxf.apache.org/jaxws"
@H_404_23@xsi:schemaLocation="http://www.springframework.org/schema/beans
@H_404_23@http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
@H_404_23@http://cxf.apache.org/jaxws
@H_404_23@http://cxf.apache.org/schemas/jaxws.xsd">
@H_404_23@@H_404_23@
<importresource="classpath:meta-inf/cxf/cxf.xml"/>
@H_404_23@<importresource="classpath:meta-inf/cxf/cxf-servlet.xml"/>
@H_404_23@@H_404_23@
<jaxws:endpointid="helloWorld"
@H_404_23@implementorClass="com.huawei.framework.webservice.HelloWorldImpl"
@H_404_23@address="/HelloWorld"/>
@H_404_23@@H_404_23@
</beans>
这里只写了CXF所需的配置,spring配置文件的其他内容已省略。用到的元素是<jaxws:endpoint>,implementorClass属性就是我们提供的实现类,然后address属性是这个web service对外暴露的路径
最后第4步是在web.xml中加载cxf
-
@H_404_23@
<?xmlversion="1.0"encoding="UTF-8"?>
@H_404_23@<web-appxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
@H_404_23@xmlns="http://java.sun.com/xml/ns/javaee"
@H_404_23@xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
@H_404_23@id="MyFramework"version="3.0">
@H_404_23@@H_404_23@
<display-name>MyFramework</display-name>
@H_404_23@@H_404_23@
<context-param>
@H_404_23@<param-name>contextConfigLocation</param-name>
@H_404_23@<param-value>
@H_404_23@WEB-INF/beans.xml,
@H_404_23@WEB-INF/cxf.xml
@H_404_23@</param-value>
@H_404_23@</context-param>
@H_404_23@@H_404_23@
<listener>
@H_404_23@<listener-class>
@H_404_23@org.springframework.web.context.ContextLoaderListener
@H_404_23@</listener-class>
@H_404_23@</listener>
@H_404_23@@H_404_23@
<servlet>
@H_404_23@<servlet-name>framework</servlet-name>
@H_404_23@<servlet-class>org.springframework.web.servlet.dispatcherServlet</servlet-class>
@H_404_23@<init-param>
@H_404_23@<param-name>contextConfigLocation</param-name>
@H_404_23@<param-value>WEB-INF/spring-mvc.xml</param-value>
@H_404_23@</init-param>
@H_404_23@<load-on-startup>1</load-on-startup>
@H_404_23@</servlet>
@H_404_23@@H_404_23@ @H_404_23@
<servlet-name>framework</servlet-name>
@H_404_23@<url-pattern>/</url-pattern>
@H_404_23@ @H_404_23@@H_404_23@
<servlet>
@H_404_23@<servlet-name>CXFServlet</servlet-name>
@H_404_23@<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
@H_404_23@<load-on-startup>2</load-on-startup>
@H_404_23@</servlet>
@H_404_23@@H_404_23@ @H_404_23@
<servlet-name>CXFServlet</servlet-name>
@H_404_23@<url-pattern>/webservice/*</url-pattern>
@H_404_23@ @H_404_23@@H_404_23@
</web-app>