SSM(四)WebService入門詳解

SSM(四)WebService入門詳解

前言

webservice這個不知道大家首次接觸的時候是怎麼理解的,反正我記得我當時第一次接觸這個東西的時候以爲又是一個XX框架,覺得還挺高大上。然而這一切在之後我使用過後才發現這些全都是YY。
那麼webservice到底是什麼呢,根據我自己的理解:簡單來說就像是一個公開的接口,其他系統不管你是用什麼語言來編寫的都可以調用這個接口,並可以返回相應的數據給你。就像是現在很多的天氣應用,他們肯定不會自己去搞一個氣象局之類的部門去監測天氣,大多都是直接調用一個天氣接口,然後返回天氣數據,相關應用就可以將這些信息展示給用戶了。
通常來說發佈這類接口的應用都是用一兩種語言來編寫即可,但是調用這個接口應用可能會是各種語言來編寫的,爲了滿足這樣的需求webservice出現了。

簡單來說webservice就是爲了滿足以上需求而定義出來的規範。


Spring整合CXF

在Java中實現webservice有多種方法,java本身在jdk1.7之後也對webservice有了默認的實現,但是在我們實際開發中一般還是會使用框架來,比如這裏所提到的CXF就有着廣泛的應用。
廢話我就不多說了,直接講Spring整合CXF,畢竟現在的JavaEE開發是離不開Spring了。
該項目還是基於之前的SSM進行開發的。

加入maven依賴

第一步肯定是要加入maven依賴:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<!--cxf-->
<!-- https://mvnrepository.com/artifact/org.apache.cxf/cxf-rt-frontend-jaxws -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.1.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.cxf/cxf-core -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-core</artifactId>
<version>3.1.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.cxf/cxf-rt-transports-http -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.1.6</version>
</dependency>

web.xml配置

接着我們需要配置一個CXF的servlet:

1
2
3
4
5
6
7
8
9
<!--定義一個cxf的servlet-->
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/webservice/*</url-pattern>
</servlet-mapping>

之後只要我們訪問webservice/*這個地址就會進入CXF的servlet中。

整合Spring配置

接下來是最重要的一部,用Spring整合CXF:
在這之前我有新建一個CXF的包,如下圖:
目錄結構
這裏有兩個主要類

  • HelloWorld接口。
  • 實現HelloWorld接口的HelloWorldImpl類。
    代碼如下:
    HelloWorld.java
    1
    2
    3
    4
    5
    6
    7
    8
    package com.crossoverJie.cxf;
    import javax.jws.WebService;
    @WebService
    public interface HelloWorld {
    public String say(String str);
    }

其中就只定義了一個簡單的say()方法。
HelloWorldImpl.java

1
2
3
4
5
6
7
8
9
10
11
package com.crossoverJie.cxf.impl;
import com.crossoverJie.cxf.HelloWorld;
import org.springframework.stereotype.Component;
import javax.jws.WebService;
@Component("helloWorld")
@WebService
public class HelloWorldImpl implements HelloWorld {
public String say(String str) {
return "Hello"+str;
}
}

這裏就是對say()方法的簡單實現。
接下來就是整合Spring了,由於需要使用到CXF的標籤,所以我們需要添加額外的命名路徑如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.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-servlet.xml"/>
<!-- 自動掃描webService -->
<context:component-scan base-package="com.crossoverJie.cxf" />
<!-- 定義webservice的發佈接口 -->
<jaxws:endpoint
implementor="#helloWorld"
address="/HelloWorld"
</beans>

更加具體的配置可以查看官方給出的文檔:http://cxf.apache.org/docs/how-do-i-develop-a-service.html
#helloWorld指的是我們在HelloWorldImpl類中所自定義的名字,/HelloWorld則是我們需要訪問的地址。
之後我們運行項目輸入該地址:http://127.0.0.1:8080/ssm/webservice/HelloWorld?wsdl如果出現如下界面:
xml則說明我們的webservice發佈成功了。
接下來只需要通過客戶端調用這個接口即可獲得返回結果了。


總結

以上就是一個簡單的webservice入門實例,更多的關於CXF攔截器,客戶端調用就沒有做過多介紹,後續有時間的話再接着更新。

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