JAX-WS + Spring 開發webservice


通過幾天的時間研究了下使用jax-ws來開發webservice,看了網上的一些資料總結出jax-ws的開發大概分為兩種。

以下項目使用的spring3.0,jar包可以到官網下載

第一種:使用獨立的端口(指端口可以在spring中自定義配置)

  首先說第一種方式,這種方式不需要添加額外的jar包,他使用的是JDK自帶的JWS來實現的。

web.xml文件配置:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="WebApp_ID" version="2.5">
    <display-name>JAXWSExample</display-name>
    
    <!-- applicationContext*.xml文件在src目錄下的conf文件夾中-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:conf/applicationContext*.xml</param-value>
    </context-param>
    
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!-- Log4j 日志 -->
    <listener>
        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    </listener>
    <!-- 防止內存泄露 -->   
    <listener>
        <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
    </listener>

</web-app>

applicationContext-jaxws.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">

    <context:annotation-config />
    <context:component-scan base-package="com.example.ws"></context:component-scan>
  <!-- baseAddress 的value地址以及端口號是自定義的,端口號不要為已使用過的 -->
    <bean class="org.springframework.remoting.jaxws.SimpleJaxWsServiceExporter">
        <property name="baseAddress" value="http://localhost:8088/" />
    </bean>
    
</beans>

java  Code

package com.example.ws;

import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;

import org.springframework.stereotype.Service;

// spring注解使用
@Service("exampleService")
// webservice地址使用
@WebService(serviceName="example")
// 防止jdk版本問題
@SOAPBinding(style=Style.RPC)
public class ExampleService {
    // dependency dao/service
    //@Autowired
    //private IBaseDao baseDao;

    
    @WebMethod
    public String example1 (String request){
        System.out.println(request);
        String response= request + "hello";
        return response;
    }
}

java代碼中方法寫的比較簡單,也可以將輸入參數和輸出參數寫為對象,這個根據需求來定。

以上就是第一種方法的實現方式,本地訪問地址直接是:http://localhost:8088/example?wsdl

 

 

第二種方式: 使用servlet方式,該方式使用服務器端口

  此種方式的話需要依賴於jax-ws 2.2中的jar文件,另外還需要下載額外的jaxws-spring-1.8.jar和xbean-spring-3.0.jar

  

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

  <!-- 到END處用來配置啟動spring容器 -->

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
  
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:conf/applicationContext*.xml</param-value>
    </context-param>
  <!-- END -->

  <!-- 用於配置地址欄請求路徑 -->
    <servlet>
        <servlet-name>JaxWsServlet</servlet-name>
        <servlet-class>com.sun.xml.ws.transport.http.servlet.WSSpringServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>JaxWsServlet</servlet-name>
        <url-pattern>/services/*</url-pattern>
    </servlet-mapping>

</web-app>

 

applicationContext-jaxws.xml,與之前不同之處在於,需要xml頭需要增加wss的聲明

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ws="http://jax-ws.dev.java.net/spring/core"
    xmlns:wss="http://jax-ws.dev.java.net/spring/servlet"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
    http://jax-ws.dev.java.net/spring/core
    http://jax-ws.dev.java.net/spring/core.xsd
    http://jax-ws.dev.java.net/spring/servlet
    http://jax-ws.dev.java.net/spring/servlet.xsd">
  
  <!-- 掃描spring注解 --> <context:annotation-config /> <context:component-scan base-package="com.example.ws"> </context:component-scan>
  <!-- 綁定webservice地址,需要與web.xml的地址對應 --> <wss:binding url="/services/add"> <wss:service> <ws:service bean="#exampleService" /> </wss:service> </wss:binding>

</beans>

 

java Code 

package com.example.ws;

import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;

import org.springframework.stereotype.Service;


@Service("exampleService")
// 只有此處與方法一不同 @WebService @SOAPBinding(style
=Style.RPC) public class ExampleService { @WebMethod public String example1(String request){ System.out.println(request); String response = request+ "hello"; return response; } }

 

此種方式的本地請求地址為:http://localhost:8080/JAXWsExample2/services/add?wsdl

               http://ip地址:服務器端口號/ 項目應用名/servlet定義地址?wsdl

 

以上兩種方式本人在本地使用Tomcat服務都可以測試通過

在WebSphere服務器上目前測試只有第二種可用

 


免責聲明!

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



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