java web專案(spring專案)中整合webservice ,實現對外開放介面
1
什麼是WebService?webService小示例 點此瞭解
下面進入正題:
Java web專案(spring專案)中整合webservice ,實現對外開放介面步驟:
準備:
採用與spring相容性較好的cxf來實現
選擇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)建立一個服務介面
- package com.service;
- import javax.jws.WebParam;
- import javax.jws.WebService;
- @WebService
- public interface IHelloWorld {
- public String sayHello(@WebParam(name = "arg0") String text);
- }
2)是介面實現類
- package com.service.impl;
- import javax.jws.WebService;
- import com.service.IHelloWorld;
- @WebService(endpointInterface = "com.service.IHelloWorld")
- public class HelloWorldImpl implements IHelloWorld {
- public String sayHello(String text) {
- return "Hello : " + text;
- }
- }
3)建立spring配置檔案,將服務類加入到容器中
webservice.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:p="http://www.springframework.org/schema/p"
- xmlns:jaxws="http://cxf.apache.org/jaxws"
- xmlns:cxf="http://cxf.apache.org/core"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
- http://cxf.apache.org/jaxws
- http://cxf.apache.org/schemas/jaxws.xsd">
- <import resource="classpath*:META-INF/cxf/cxf.xml" />
- <import resource="classpath*:META-INF/cxf/cxf-extension-soap.xml" />
- <import resource="classpath*:META-INF/cxf/cxf-servlet.xml" />
- <!--下面的class屬性值一定要跟你專案中服務實現類的包路徑完全一致-->
- <bean id="hello" class="com.service.impl.HelloWorldImpl"/>
- <jaxws:endpoint id="helloWorld" implementor="#hello" address="/HelloWorld" />
- </beans>
在web.xml中新增webservice.xml配置檔案
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>
- /WEB-INF/webservice.xml
- </param-value>
- </context-param>
4)在web.xml中加入cxf servlet
- <servlet>
- <display-name>CXF Servlet</display-name>
- <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>
至此,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)首先要建立一個和伺服器端一樣的服務介面,(如果客戶端和伺服器端在同一個專案中則可以省略這步)
- package com.service;
- import javax.jws.WebParam;
- import javax.jws.WebService;
- @WebService
- public interface IHelloWorld {
- public String sayHello(@WebParam(name = "arg0") String text);
- }
2)建立spring-client.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:p="http://www.springframework.org/schema/p"
- xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:cxf="http://cxf.apache.org/core"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
- http://cxf.apache.org/jaxws
- http://cxf.apache.org/schema/jaxws.xsd">
- <bean id="client" class="com.service.IHelloWorld" factory-bean="clientFactory" factory-method="create" />
- <bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
- <property name="serviceClass" value="com.service.IHelloWorld" />
- <property name="address" value="http://localhost:8080/test/HelloWorld" />
- </bean>
- </beans>
3)測試類
- package com.test;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- import com.service.IHelloWorld;
- public class Test {
- public static void main(String[] args) {
- ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-client.xml");
- IHelloWorld client = (IHelloWorld) ctx.getBean("client");
- String result = client.sayHello("你好!");
- System.out.println(result);
- }
- }
執行成功後顯示
Hello:你好!