java web項目(spring項目)中集成webservice ,實現對外開放接口


java web項目(spring項目)中集成webservice ,實現對外開放接口

 

什么是WebService?webService小示例 點此了解

下面進入正題:

Java web項目(spring項目)中集成webservice ,實現對外開放接口步驟:

准備:

采用與spring兼容性較好的cxf來實現

cxf 的  jar下載地址: http://cxf.apache.org/download.html

選擇zip格式下載,解壓后的lib目錄下的jar

需要最少的jar如下:

cxf-2.3.3.jar
geronimo-annotation_1.0_spec-1.1.1.jar
geronimo-jaxws_2.2_spec-1.0.jar
geronimo-stax-api_1.0_spec-1.0.1.jar
geronimo-ws-metadata_2.0_spec-1.1.3.jar
jaxb-api-2.2.1.jar
jaxb-impl-2.2.1.1.jar
neethi-2.0.4.jar
wsdl4j-1.6.2.jar
XmlSchema-1.4.7.jar
wstx-asl-3.2.9.jar

一:創建webservice服務器

1)創建一個服務接口

[java]  view plain  copy
 
  1. package com.service;  
  2.   
  3. import javax.jws.WebParam;  
  4. import javax.jws.WebService;  
  5.   
  6. @WebService  
  7. public interface IHelloWorld {  
  8.     public String sayHello(@WebParam(name = "arg0") String text);  
  9. }  

2)是接口實現類

[java]  view plain  copy
 
  1. package com.service.impl;  
  2.   
  3. import javax.jws.WebService;  
  4.   
  5. import com.service.IHelloWorld;  
  6.   
  7. @WebService(endpointInterface = "com.service.IHelloWorld")  
  8. public class HelloWorldImpl implements IHelloWorld {  
  9.     public String sayHello(String text) {  
  10.         return "Hello : " + text;  
  11.     }  
  12. }  


3)創建spring配置文件,將服務類加入到容器中

webservice.xml

[html]  view plain  copy
 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  4.     xmlns:p="http://www.springframework.org/schema/p"  
  5.     xmlns:jaxws="http://cxf.apache.org/jaxws"  
  6.     xmlns:cxf="http://cxf.apache.org/core"  
  7.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  8.     http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  9.     http://cxf.apache.org/jaxws  
  10.     http://cxf.apache.org/schemas/jaxws.xsd">  
  11.   
  12.     <import resource="classpath*:META-INF/cxf/cxf.xml" />  
  13.     <import resource="classpath*:META-INF/cxf/cxf-extension-soap.xml" />  
  14.     <import resource="classpath*:META-INF/cxf/cxf-servlet.xml" />  
  15.   
  16.     <!--下面的class屬性值一定要跟你項目中服務實現類的包路徑完全一致-->  
  17.     <bean id="hello" class="com.service.impl.HelloWorldImpl"/>  
  18.     <jaxws:endpoint id="helloWorld" implementor="#hello" address="/HelloWorld" />  
  19. </beans>  

在web.xml中添加webservice.xml配置文件

[html]  view plain  copy
 
  1. <context-param>  
  2.     <param-name>contextConfigLocation</param-name>  
  3.     <param-value>  
  4.         /WEB-INF/webservice.xml       
  5.     </param-value>  
  6. </context-param>  

4)在web.xml中加入cxf servlet

[html]  view plain  copy
 
  1. <servlet>  
  2.     <display-name>CXF Servlet</display-name>  
  3.     <servlet-name>CXFServlet</servlet-name>  
  4.     <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>  
  5.     <load-on-startup>1</load-on-startup>  
  6. </servlet>  
  7.   
  8. <servlet-mapping>  
  9.     <servlet-name>CXFServlet</servlet-name>  
  10.     <url-pattern>/webservice/*</url-pattern>  
  11. </servlet-mapping>  

至此,webservice服務器就創建好了,

在瀏覽器中訪問:http://localhost:8080/test/webservice/HelloWorld?wsdl,(test是項目名稱)。如果出現了類似與

<wsdl:definitions xmlns:ns1="http://service.com/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://impl.service.com/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="HelloWorldImplService" targetNamespace="http://impl.service.com/">
。。。。

就配置成功了。

接下來貼幾個運行時的錯誤解決方法

1:webservice.xml中提示cxf.xml,cxf-servlet.xml not found,我在上面寫的路徑是classpath*:META-INF/cxf/cxf.xml,這里的classpath后面還跟了一個“*”符號,沒加符號表示只在類路徑下查找cxf.xml文件,加了號表示不僅在類路徑下查找xml文件,還在jar包中查找xml文件。所以當我們在項目類路徑下沒有假如cxf.xml等配置文件時,就一定要在classpath后加*,這樣spring容器才會去所加入的jar包中找。

2:假如cxf servlet時,映射路徑不要寫成/*,否則會出現訪問不了項目主頁的情況,可以寫成/webservice/*或者別的項目中沒有使用過的路徑來作為cxf servlet的請求路徑。

 

二:創建webservice客戶端

客戶端可以和服務器放在同一個項目中用來測試,也可以新建一個Java項目來進行測試。

新建一個Java項目測試時,要假如對應的jar包,跟服務器一樣,使用spring還要假如spring jar包。

我在這里新建一個項目,依舊使用spring來測試

1)首先要創建一個和服務器端一樣的服務接口,(如果客戶端和服務器端在同一個項目中則可以省略這步)

[java]  view plain  copy
 
  1. package com.service;  
  2.   
  3. import javax.jws.WebParam;  
  4. import javax.jws.WebService;  
  5.   
  6. @WebService  
  7. public interface IHelloWorld {  
  8.     public String sayHello(@WebParam(name = "arg0") String text);  
  9. }  

2)創建spring-client.xml,

[html]  view plain  copy
 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"  
  4.     xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:cxf="http://cxf.apache.org/core"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans   
  6.     http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  7.     http://cxf.apache.org/jaxws   
  8.     http://cxf.apache.org/schema/jaxws.xsd">  
  9.   
  10.     <bean id="client" class="com.service.IHelloWorld" factory-bean="clientFactory" factory-method="create" />  
  11.     <bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">  
  12.         <property name="serviceClass" value="com.service.IHelloWorld" />  
  13.         <property name="address" value="http://localhost:8080/test/HelloWorld" />  
  14.     </bean>  
  15. </beans>  

3)測試類

 

[java]  view plain  copy
 
  1. package com.test;  
  2.   
  3. import org.springframework.context.ApplicationContext;  
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  5.   
  6. import com.service.IHelloWorld;  
  7.   
  8. public class Test {  
  9.     public static void main(String[] args) {  
  10.         ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-client.xml");  
  11.         IHelloWorld client = (IHelloWorld) ctx.getBean("client");  
  12.         String result = client.sayHello("你好!");  
  13.         System.out.println(result);  
  14.     }  
  15. }  

 

運行成功后顯示

Hello:你好!

 

文章參考: http://www.open-open.com/lib/view/open1405929509210.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM