SSM集成webservice-絕對能用

ssm集成webservice,我這裏用的是CXF,因爲對於Axis和Afire來說CXF算是非常好的,這裏就不對他們的區別多說了,首先:

1、加入相應的依賴:

<!--webServiceCxf  -->
        <!-- https://mvnrepository.com/artifact/org.apache.cxf/cxf-core -->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-core</artifactId>
            <version>3.2.7</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxws</artifactId>
            <version>3.2.7</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxrs</artifactId>
            <version>3.2.7</version>
        </dependency>
        <dependency> 
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http</artifactId>
            <version>3.2.7</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.cxf/cxf-rt-frontend-simple -->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-simple</artifactId>
            <version>3.2.7</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.cxf/cxf-rt-wsdl -->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-wsdl</artifactId>
            <version>3.2.7</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.cxf/cxf-rt-bindings-soap -->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-bindings-soap</artifactId>
            <version>3.2.7</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.ws.xmlschema/xmlschema-core -->
        <dependency>
            <groupId>org.apache.ws.xmlschema</groupId>
            <artifactId>xmlschema-core</artifactId>
            <version>2.2.4</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.cxf/cxf-rt-databinding-jaxb -->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-databinding-jaxb</artifactId>
            <version>3.2.7</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.cxf/cxf-rt-ws-addr -->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-ws-addr</artifactId>
            <version>3.2.7</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.cxf/cxf-rt-ws-policy -->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-ws-policy</artifactId>
            <version>3.2.7</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.neethi/neethi -->
        <dependency>
            <groupId>org.apache.neethi</groupId>
            <artifactId>neethi</artifactId>
            <version>3.1.1</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/wsdl4j/wsdl4j -->
        <dependency>
            <groupId>wsdl4j</groupId>
            <artifactId>wsdl4j</artifactId>
            <version>1.6.3</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.codehaus.woodstox/stax2-api -->
        <dependency>
            <groupId>org.codehaus.woodstox</groupId>
            <artifactId>stax2-api</artifactId>
            <version>3.1.1</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.codehaus.woodstox/woodstox-core-asl -->
        <dependency>
            <groupId>org.codehaus.woodstox</groupId>
            <artifactId>woodstox-core-asl</artifactId>
            <version>4.4.1</version>
        </dependency>
        <dependency>
          <groupId>com.qcloud</groupId>
          <artifactId>qcloud-image-sdk</artifactId>
          <version>2.3.3</version>
        </dependency>

2、在web.xml里加上webservice的servlet

<!--註冊一個用於接收其他工程向本工程發送的webservice請求的請求接收器-->
      <servlet>
        <servlet-name>CXFServlet</servlet-name>
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
      </servlet>
      <!--配置過濾請求地址項目名+webService+address+?wsdl-->
      <servlet-mapping>
        <servlet-name>CXFServlet</servlet-name>
        <url-pattern>/webService/*</url-pattern>
      </servlet-mapping>

3、在spring.xml加入相應的配置

         xmlns:jaxws="http://cxf.apache.org/jaxws"
         xmlns:soap="http://cxf.apache.org/bindings/soap"

         http://cxf.apache.org/jaxws
         http://cxf.apache.org/schemas/jaxws.xsd

這是圖中的配置,位置最好一樣,因爲ssm框架太操蛋。

4、開始寫接口:

import java.util.List;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;

import com.yltd.cnyun.common.area.entity.Area;
/**
 * 
 * 基於soap的服務
 */
@WebService
public interface TestService {

     // 提供一個對外公開的服務
     @WebMethod(operationName = "sayHellow")
     public String sayHellow(@WebParam(name = "username") String username);

}

實現類:

import java.util.List;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;

import org.springframework.beans.factory.annotation.Autowired;

import com.yltd.cnyun.common.area.entity.Area;
import com.yltd.cnyun.common.area.service.AreaService;

/**
 * 服務實現類
 */
@WebService    
public class TestServiceImpl implements TestService {

    @Autowired
    AreaService areaService;
    
    
    @WebMethod(operationName = "sayHellow")
    @Override
    public String sayHellow(@WebParam(name = "username")String username) {
        // TODO Auto-generated method stub
        return username+"你好!";
    }
}

5、把寫好的接口配置在spring.xml中,項目啓動的時候掃描spring.xml自動發佈出去

<!-- 配置方式1   注意:serviceClass爲接口類並非實現類 -->
    <!-- <jaxws:server serviceClass="com.yltd.cnyun.common.webservice.service.TestService" address="/webService"></jaxws:server> -->
    
    <!-- 配置方式2    注意:implementor爲接口的具體實現類 -->
    <jaxws:endpoint implementor="com.yltd.cnyun.common.webservice.service.TestServiceImpl" address="webService" ></jaxws:endpoint>

兩個方式都可以

然後啓動項目,訪問路徑localhost:8080/項目名字/webSerive/webService?wsdl出現,

 

說明你發佈成功了。

6、最後一步測試你發佈的接口:

@SuppressWarnings("unchecked")
    public static void main(String[] args) throws Exception {
        
        System.out.println("開始調用接口!");
        Properties props = System.getProperties(); 
        props.setProperty("org.apache.cxf.stax.allowInsecureParser", "1"); 
        props.setProperty("UseSunHttpHandler", "true");
        //採用動態工廠方式 不需要指定服務接口
        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
        Client client = dcf.createClient("http://10.10.26.108:8080/cnyun-web/webService/webService?wsdl");
        Object[] res = client.invoke("sayHellow", "admin");
        System.out.println("Echo response: sayHellow" + res[0]);

}

}

console不報錯有數據,就說明你調通了,這裏客戶端調服務端的時候,我寫的是java程序測試用的,相應的jar包比較多,圖片給你貼出來:

大部分的包是沒用的,我去官網把apache.cxf所有的包都到進來了,你用的時候自己刪除吧,可能會出現包的版本不一樣報錯的問題,這都是小問題了,自己搞一下,哪看不懂的可以私信。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章