使用Spring+CXF開發WebService,使用註解方式__web
來源:互聯網
上載者:User
Apache CXF 提供方便的 Spring 整合方法,可以通過註解、 Spring 標籤式配置來暴露 Web Services 和消費 Web Services
各種類型的Annotation 。 @WebService 和 @WebMethod 是 WSDL 映射 Annatotion 。這些 Annotation 將描述 Web Service 的 WSDL 文件項目和 Java 原始碼聯絡在一起。 @SOAPBinding 是一個綁定的 annotation 用來說明網路通訊協定和格式。
1、 @WebService annotation 的元素 name,serviceName 和 targetNamespace 成員用來描述
wsdl:portType, wsdl:service ,和 targetNameSpace 產生 WebService 中的 WSDL 檔案。
2、 @SOAPBinding 是一個用來描述 SOAP 格式和 RPC 的協議的綁定 Annotation 。
3、 @WebMethod Annotation 的 operationName 成員描述了 wsdl:operation ,而且它的操作描
述了WSDL 文檔中的 SOAPAction 頭部。這是用戶端必須要放入到 SQAPHeader 中的數
值,SOAP 1.1 中的一種約束。
4、 @WebParam Annotation 的 partName 成員描述了 WSDL 文檔中的 wsdl:part 。
5、 @WebResult Annotation 的 partName 成員描述了 wsdl:part 用來返回 WSDL 文檔的值。
例如下面使用annotation 定義了一個 webservice :
import java.util.List;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import com.cxf.pojo.User;
@WebService (targetNamespace = "http://jdk.study.hermit.org/client" )
public interface UserService {
@WebMethod (operationName= "Insert" )
public void insert( @WebParam (name = "userId" ) String userid,
@WebParam (name = "userName" ) String username,
@WebParam (name = "userEmail" ) String useremail,
@WebParam (name = "userAge" ) int userage);
@WebMethod (operationName= "GetUserById" )
@WebResult (name = "result" )
public User getUserById( @WebParam (name= "userid" ) String userid);
@WebMethod (operationName= "GetAllUsers" )
@WebResult (name = "result" )
public List getAllUsers();
}
其實作類別如下所示:
import java.util.List;
import javax.jws.WebService;
import com.cxf.dao.UserDao;
import com.cxf.pojo.User;
import com.cxf.service.UserService;
@WebService (endpointInterface= "com.cxf.service.UserService" )
public class UserServiceImpl implements UserService {
private UserDao userDao ;
public List getAllUsers() {
return userDao .findAllUser();
}
public User getUserById(String userid) {
return userDao .findUserById(userid);
}
public void insert(String userid, String username, String useremail, int userage) {
User user= new User();
user.setUserage(userage);
user.setUseremail(useremail);
user.setUserid(userid);
user.setUsername(username);
userDao .insert(user);
System. out .println( "insert successfully!" );
}
public void setUserDao(UserDao userDao) {
this . userDao = userDao;
}
}
注意:實作類別中的@WebService ,其中的 endpointInterface 成員指定了該類實現的介面
在Spring 的設定檔中,需要對其進行配置:
首先在ApplicationContext.xml(Spring 的設定檔 ) 中引入 CXF 的 XML Scheam 設定檔 ), 如下 :
< beans xmlns = "http://www.springframework.org/schema/beans"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation = "http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd ">
<! — 還需要引入以下3 個關於 CXF 的資源檔,這三個檔案在 cxf.jar 中 -->
< 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" />
…… …… …… …… …… …… …… …… …… ……
</bean>
其次就是在 Spring的設定檔中配置 webservice ,如下所示:
< jaxws:endpoint id = "userManager" address = "/UserManager"
implementorClass = "com.cxf.service.UserService" >
< jaxws:implementor >
< bean id = "userServiceImpl"
class = "com.cxf.service.impl.UserServiceImpl" >
< property name = "userDao" >
< ref bean = "userDao" />
</ property >
</ bean >
</ jaxws:implementor >
</ jaxws:endpoint >
注意:
①、 address 為webservice 發布的地址
②、 implementorClass 為該webservice 實現的介面
③、 < jaxws:implementor ></ jaxws:implementor > 之間定義的是 implementorClass 指定介面的實作類別
另外,在Spring 的設定檔中配置完成後,需要修改 web.xml 檔案
< 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 > /services/* </ url-pattern >
</ servlet-mapping >
注意<url-pattern>/ services /*</url-pattern>配置, CXFServlet 會攔截此類 url ,進行處理 。上面配置的webservice 將通過如下 URL 訪問到:
http://localhost:8080/cxf-ws/services/ UserManager
UserManager 為 < jaxws:endpoint > 標籤中address 屬性對應的值
cxf-ws 為本項目的名稱
配置完成之後,將項目部署到tomcat 上
輸入URL : http://localhost:8080/cxf-ws/services/ UserManager ?wsdl
將會看到產生的wsdl 檔案
為了能夠直接存取到 com.cxf.service.UserService 中的 insert 方法,進行如下測試:
建立一個insertUser.html 頁面:
< html >
< head >
< script type = "text/javascript" >
function post() {
var xmlhttp= false ;
if (!xmlhttp && typeof XMLHttpRequest!= 'undefined' ) {
try {
xmlhttp = new XMLHttpRequest();
} catch (e) {
xmlhttp= false ;
}
}
if (!xmlhttp && window.createRequest) {
try {
xmlhttp = window.createRequest();
} catch (e) {
xmlhttp= false ;
}
}
var dest = document.getElementById( 'destination' ).value;
xmlhttp.open( "POST" , dest, true );
xmlhttp.onreadystatechange= function () {
if (xmlhttp.readyState==4) {
document.getElementById( "result" ).innerHTML = xmlhttp.responseText;
}
}
xmlhttp.setRequestHeader( "Man" , "POST " + dest + " HTTP/1.1" )
xmlhttp.setRequestHeader( "MessageType" , "CALL" )
xmlhttp.setRequestHeader( "Content-Type" , "text/xml" );
var texta = document.getElementById( 'texta' );
var soapmess = texta.value;
xmlhttp.send(soapmess);
}
</ script >
</ head >
< body >
hi there
< form id = "forma" >
< textarea id = "texta" rows = "10" cols = "80" >
<?xml version='1.0' encoding='UTF-8'?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance" xmlns:xsd="http://www.w3.org/1999/XMLSchema">
<SOAP-ENV:Body>
< I nsert>
<userId>2005221104210066</userId>
<userName>Leon Cao</userName>
<userEmail>caohongliang2@163.com</userEmail>
<userAge>23</userAge>
</ I nsert>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
</ textarea >
< p >
< input type = "text" id = "destination" size = "50" value = " http://localhost:8080/cxf-ws/services/UserManager " ></ input >
< p >
< input type = "button" onclick = "post()" name = "submit" value = "Submit" ></ input >
</ form >
< div id = "container" ></ div >
Result:
< hr >
< div id = "result" >
</ div >
< hr >
< div id = "soap" >
</ div >
</ body >
</ html >
在該測試頁面需要注意標記為紅色的部分,如下是一個soap message :
<?xml version='1.0' encoding='UTF-8'?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance" xmlns:xsd="http://www.w3.org/1999/XMLSchema">
<SOAP-ENV:Body>
< I nsert>
<userId>2005221104210066</userId>
<userName>Leon Cao</userName>
<userEmail>caohongliang2@163.com</userEmail>
<userAge>23</userAge>
</ I nsert>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
注意:
① <SOAP-ENV:Body > 標籤之間 < I nsert> 對應 @WebMethod (operationName= "Insert" )
中的operationName 屬性的值
② <userId> 、 <userName> 、 <userEmail> 、 <userAge> 標籤對各自對應著insert 方法中
@WebParam 中name 屬性的值:
@WebParam (name = "userId" ) String userid,
@WebParam (name = "userName" ) String username,
@WebParam (name = "userEmail" ) String useremail,
@WebParam (name = "userAge" ) int userage);
當向webservice 上發送該 soap 訊息的時候將自動解析該 soap 訊息,將 Insert 解析為 insert 方法,將 <userId> 、 <userName> 、 <userEmail> 、 <userAge> 標籤對之間的值解析為相對應的參數 userid 、 username 、 useremail 、 userage
③ < input type = "text" id = "destination" size = "50" value = " http://localhost:8080/cxf-ws/services/UserManager " ></ input >
該文字標籤中的value 屬性為 webservice 的地址
運行該頁面,點擊Submit 後將在資料庫中插入一行資料,即調用了 inser 方法成功