使用Apache CXF開發Web Service步驟
使用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地址