使用ApacheCXF开发Web Service步骤
用CXF构建webservice程序的大致流程如下:
一.服务端
1.配置web.xml文件,加载CXF
2.编写java接口和接口的实现
3.在spring的applicationContext.xml中配置接口和实现类,并发布为服务
二.客户端
1. 在spring的applicationContext.xml中配置服务(注意url)
2.编写服务端一样Java接口与之对应接收
3.在服务层调用
注意:对象传输时使用dto模式
示例:
服务端
1.web.xml
<?xml version="1.0"encoding="UTF-8"?>
<!DOCTYPE web-app
PUBLIC"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/applicationContext.xml </param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet>
<servlet-name>CXFServlet</servlet-name>
<display-name>CXF Servlet</display-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>/services/*</url-pattern>
</servlet-mapping>
</web-app>
2.接口HelloWord.java和接口的实现HelloWordImpl.java
package com.demo;
import javax.jws.WebService;
/** *//**
* service interface
*
* @author
*
*/
@WebService
public interface HelloWord {
StringsayHi(String text);
}
package com.demo;
import javax.jws.WebService;
/** *//**
* implementation
*
* @author
*
*/
@WebService
public class HelloWordImpl implements HelloWord {
public StringsayHi(String text) {
return"Hi" + text;
}
}
3.applicationContext.xml
<?xml version="1.0"encoding="UTF-8"?>
<beansxmlns="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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
<importresource="classpath:META-INF/cxf/cxf.xml" />
<importresource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
<importresource="classpath:META-INF/cxf/cxf-servlet.xml" />
<jaxws:endpoint id="helloWord"
implementor="com.demo.HelloWordImpl"address="/HelloWord" />
</beans>
客户端
1.applicationContext.xml
<?xml version="1.0"encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jaxws="http://cxf.apache.org/jaxws"xmlns:cxf="http://cxf.apache.org/core"
xsi:schemaLocation="http://cxf.apache.org/jaxwshttp://cxf.apache.org/schemas/jaxws.xsd http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsdhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsd"
default-autowire="byType"default-lazy-init="true">
<description>WebService客户端配置</description>
<import resource="classpath:META-INF/cxf/cxf.xml"/>
<importresource="classpath:META-INF/cxf/cxf-servlet.xml" />
<importresource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
<jaxws:client id="userWebService"serviceClass="com.demo.HelloWord "
address="http://localhost:8080/userCenter/services/HelloWord">
</jaxws:client>
</beans>
2.接口HelloWord.java
package com.demo;
import javax.jws.WebService;
/** *//**
* service interface
*
* @author
*
*/
@WebService
public interface HelloWord {
StringsayHi(String text);
}
3.编写一个服务层类调用
package com.demo;
import com.demo.HelloWord;
public class HelloWordService {
private HelloWordhelloWord
public voidset(HelloWord helloWord) {
this.helloWord=helloWord;
}
public String getService(Strings){
returnhelloWord.sayHi(s)
}
}
wsdl2java生成客户端方法:wsdl2java -d d:\src -client wsdl地址