[轉] WebService開發筆記 1 -- 利用cxf開發WebService竟然如此簡單

來源:互聯網
上載者:User

標籤:blog   http   ar   io   os   使用   sp   for   java   

以下文章來自   http://www.blogjava.net/jacally/articles/186655.html

 

 

現在的項目中需要用到SOA概念的地方越來越多,最近我接手的一個項目中就提出了這樣的業務要求,需要在.net開發的用戶端系統中訪問java開發的web系統,這樣的業務需求自然需要通過WebService進行資訊資料的操作。下面就將我們在開發中摸索的一點經驗教訓總結以下,以供大家參考. 

我們項目的整個架構使用的比較流行的WSH MVC組合,即webwork2 + Spring + Hibernate; 
1.首先整合Apacha CXF WebService 到 Spring 架構中; 
apache cxf :http://people.apache.org/dist/incubator/cxf/2.0.4-incubator/apache-cxf-2.0.4-incubator.zip 
在spring context設定檔中引入以下cxf配置 

<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" />


在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>



2.開發服務端WebService介面: 

/**
* WebService介面定義類.
*
* 使用@WebService將介面中的所有方法輸出為Web Service.
* 可用annotation對設定方法、參數和傳回值在WSDL中的定義.
*/
@WebService
public interface WebServiceSample {


/**
* 一個簡單的方法,返回一個字串
* @param hello
* @return
*/
String say(String hello);

/**
* 稍微複雜一些的方法,傳遞一個對象給服務端處理
* @param user
* @return
*/
String sayUserName(
@WebParam(name = "user")
UserDTO user);

/**
* 最複雜的方法,返回一個List封裝的對象集合
* @return
*/
public
@WebResult(partName="o")
ListObject findUsers();

}


由簡單到複雜定義了三個介面,類比業務需求; 

3.實現介面 

/**
* WebService實作類別.
*
* 使用@WebService指向Interface定義類即可.
*/
@WebService(endpointInterface = "cn.org.coral.biz.examples.webservice.WebServiceSample")
public class WebServiceSampleImpl implements WebServiceSample {

public String sayUserName(UserDTO user) {
return "hello "+user.getName();
}

public String say(String hello) {
return "hello "+hello;
}

public ListObject findUsers() {
ArrayList<Object> list = new ArrayList<Object>();

list.add(instancUser(1,"lib"));
list.add(instancUser(2,"mld"));
list.add(instancUser(3,"lq"));
list.add(instancUser(4,"gj"));
ListObject o = new ListObject();
o.setList(list);
return o;
}

private UserDTO instancUser(Integer id,String name){
UserDTO user = new UserDTO();
user.setId(id);
user.setName(name);
return user;
}
}



4.依賴的兩個類:使用者物件與List對象 

/**
* Web Service傳輸User資訊的DTO.
*
* 分離entity類與web service介面間的耦合,隔絕entity類的修改對介面的影響.
* 使用JAXB 2.0的annotation標註JAVA-XML映射,盡量使用預設約定.
*
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "User")
public class UserDTO {

protected Integer id;

protected String name;

public Integer getId() {
return id;
}

public void setId(Integer value) {
id = value;
}

public String getName() {
return name;
}

public void setName(String value) {
name = value;
}
}


關於List對象,參照了有關JWS的一個問題中的描述:DK6.0 內建的WebService中 WebMethod的參數好像不能是ArrayList 或者其他List 
傳遞List需要將List 封裝在其他對象內部才行 (個人理解 如有不對請指出) ,我在實踐中也遇到了此類問題.通過以下封裝的對象即可以傳遞List對象. 

/**
* <p>Java class for listObject complex type.
*
* <p>The following schema fragment specifies the expected content contained within this class.
*
* <pre>
* <complexType name="listObject">
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <sequence>
* <element name="list" type="{http://www.w3.org/2001/XMLSchema}anyType" maxOccurs="unbounded" minOccurs="0"/>
* </sequence>
* </restriction>
* </complexContent>
* </complexType>
* </pre>
*
*
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "listObject", propOrder = { "list" })
public class ListObject {

@XmlElement(nillable = true)
protected List<Object> list;

/**
* Gets the value of the list property.
*
* <p>
* This accessor method returns a reference to the live list,
* not a snapshot. Therefore any modification you make to the
* returned list will be present inside the JAXB object.
* This is why there is not a <CODE>set</CODE> method for the list property.
*
* <p>
* For example, to add a new item, do as follows:
* <pre>
* getList().add(newItem);
* </pre>
*
*
* <p>
* Objects of the following type(s) are allowed in the list
* {@link Object }
*
*
*/
public List<Object> getList() {
if (list == null) {
list = new ArrayList<Object>();
}
return this.list;
}

public void setList(ArrayList<Object> list) {
this.list = list;
}

}



5.WebService 服務端 spring 設定檔 ws-context.xml 

<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://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"
default-autowire="byName" default-lazy-init="true">

<jaxws:endpoint id="webServiceSample"
address="/WebServiceSample" implementor="cn.org.coral.biz.examples.webservice.WebServiceSampleImpl"/>

</beans>



WebService 用戶端 spring 設定檔 wsclient-context.xml 

<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://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"
default-autowire="byName" default-lazy-init="true">

<!-- ws client -->
<bean id="identityValidateServiceClient" class="cn.org.coral.admin.service.IdentityValidateService"
factory-bean="identityValidateServiceClientFactory" factory-method="create" />

<bean id="identityValidateServiceClientFactory"
class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
<property name="serviceClass"
value="cn.org.coral.admin.service.IdentityValidateService" />
<property name="address"
value="http://88.148.29.54:8080/coral/services/IdentityValidateService"/>
</bean>

</beans>


6.發布到tomcat伺服器以後通過以下地址即可查看自訂的webservice介面產生的wsdl: 
http://88.148.29.54:8080/aio/services/WebServiceSample?wsdl 

7.調用WebService介面的Junit單元測試程式 

package test.coral.sample;

import org.springframework.test.AbstractDependencyInjectionSpringContextTests;

import cn.org.coral.biz.examples.webservice.WebServiceSample;
import cn.org.coral.biz.examples.webservice.dto.UserDTO;

public class TestWebServiceSample extends
AbstractDependencyInjectionSpringContextTests {
WebServiceSample webServiceSampleClient;

public void setWebServiceSampleClient(WebServiceSample webServiceSampleClient) {
this.webServiceSampleClient = webServiceSampleClient;
}

@Override
protected String[] getConfigLocations() {
setAutowireMode(AUTOWIRE_BY_NAME);
//spring 用戶端設定檔儲存位置
return new String[] { "classpath:/cn/org/coral/biz/examples/webservice/wsclient-context.xml" };
}

public void testWSClinet(){
Assert.hasText(webServiceSampleClient.say(" world"));
}
}

[轉] WebService開發筆記 1 -- 利用cxf開發WebService竟然如此簡單

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.