cxf-spring-pratice-service
其實吧,這篇博客基本上就是照搬了官方的How-to.
2017/11/15 時隔六個月, 重新下組織格式和內容.
諸如webservice,WSDL的定義及作用,還有意義啥的咱就不再廢話了,直接上干貨。
1. 引入cxf相關的依賴
1.1 使用maven
<dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-frontend-jaxws</artifactId><version>3.1.1</version> </dependency> <dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-transports-http</artifactId><version>3.1.1</version> </dependency>1.2 直接引入相關jar
真心不建議采用這種方式!
asm-5.0.4.jar cxf-core-3.1.1.jar cxf-rt-bindings-soap-3.1.1.jar cxf-rt-bindings-xml-3.1.1.jar cxf-rt-databinding-jaxb-3.1.1.jar cxf-rt-frontend-jaxws-3.1.1.jar cxf-rt-frontend-simple-3.1.1.jar cxf-rt-transports-http-3.1.1.jar cxf-rt-ws-addr-3.1.1.jar cxf-rt-ws-policy-3.1.1.jar cxf-rt-wsdl-3.1.1.jar jaxb-core-2.2.11.jar jaxb-impl-2.2.11.jar neethi-3.0.3.jar stax2-api-3.1.4.jar woodstox-core-asl-4.4.1.jar wsdl4j-1.6.3.jar xml-resolver-1.2.jar xmlschema-core-2.2.1.jar2. 創建相關類
2.1 IHelloWorld接口
@WebService interface IHelloWorld {String sayHello(@WebParam(name = "username") String username);Map<String, Object> getObj(@WebParam(name = "username")String username, @WebParam(name = "sexy")String sexy); }2.2 HelloWorldImpl實現類
public class HelloWorldImpl implements IHelloWorld { @Override public String sayHello(String username) { return "Hello " + username; } @Overridepublic Map<String, Object> getObj(String username, String sexy) {Map<String, Object> customer = new HashMap<String, Object>();customer.put("NAME", username);customer.put("SEXY", sexy);customer.put("BIRTHDAY", Calendar.getInstance().getTime());return customer;} }3. Spring配置
spring-cxf-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:http-conf="http://cxf.apache.org/transports/http/configuration"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd http://cxf.apache.org/transports/http/configuration http://cxf.apache.org/schemas/configuration/http-conf.xsd"><!-- 以下這個文件位于cxf-core-3.1.1.jar中 --><import resource="classpath:META-INF/cxf/cxf.xml" /><!-- 以下這個文件位于cxf-rt-databinding-jaxb-3.1.1.jar中 --><import resource="classpath:META-INF/cxf/cxf-servlet.xml" /><jaxws:endpoint id="helloService"implementor="com.kq.webservice.impl.HelloWorldImpl"address="/helloService" /> </beans>4. web.xml
<!-- 載入上面配置的spring-cxf-service.xml文件 --> <context-param><param-name>contextConfigLocation</param-name><param-value>classpath:config/spring.xml;<!-- cxf配置,必須在這里;這里涉及到的是SpringMVC中child,parent container的加載問題 -->classpath:config/spring-cxf-service.xml; </param-value> </context-param><!-- 配置cxf相關的servlet --> <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>/webservice/*</url-pattern> </servlet-mapping>5. 訪問
獲取該路徑所有的服務列表
// 格式 http://host:port/context_path/services// sample http://localhost:9090/SpringMVCDemo/webservice讀取wsdl文件內容
// 格式 http://host:port/context_path/services/serviceName?wsdl// sample http://localhost:9090/SpringMVCDemo/webservice/helloService?wsdl6. 測試
6.1 單獨的測試項目
6.2 基于SoapUI進行接口測試
6.3 編寫測試類
此種方式比較方便,無須配置比較靈活,大部分情況下都會采用這種方式調用服務程序。
返回簡單的string
private static void test_returnSimpleString(){JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();factory.setServiceClass(IHelloWorld.class);factory.setAddress("http://localhost:8080/JYXT/webservice/helloService");IHelloWorld client = (IHelloWorld) factory.create();String response = client.sayHello("LQ");System.out.println("Response:" + response); }返回復雜對象
private static void test_returnCustomObj(){JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();factory.setServiceClass(IHelloWorld.class);factory.setAddress("http://localhost:8080/JYXT/webservice/helloService");IHelloWorld client = (IHelloWorld) factory.create();Map<String, Object> obj = client.getObj("LQ","1");System.out.println(obj); }返回JSON
未完成7. 疑難問題
Exception in thread "main" **org.apache.cxf.common.i18n.UncheckedException**: No operation was found with the name {http://impl.server.test.com/}helloWorld.
解決方法:對服務端的接口實現類中的@WebService添加targetNamespace,其值為接口包名的倒置。例如我的IHelloWorld接口所在的包為com.test.server,此時對應的targeNamespace的值為http://server.test.com/
@WebService(endpointInterface = "com.test.server.IHelloWorld", serviceName="helloWorld", targetNamespace="http://server.test.com/") public class HelloWorldImp implements IHelloWorld {public String helloWorld(String name) {return name+" Hello,World!";}}8. 參考鏈接
http://cxf.apache.org/docs/writing-a-service-with-spring.html - 官方文檔
http://blog.csdn.net/blueheart20/article/details/42971713
總結
以上是生活随笔為你收集整理的cxf-spring-pratice-service的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: PHP实现量化交易,量化交易干货丨如何使
- 下一篇: 从零开始制作基于Unity引擎的宝石消消