以Spring WS建立Web Service

以Spring WS建立Web Service

1. 設定web.xml
<servlet-name>spring-ws</servlet-name>是有用處的,所需的Spring設定文件的名稱必須用xxx-servlet.xml的格式,
其中xxx就是servlet-name的值,即常規下名字為spring-ws-servlet.xml,而且該文件會自動尋找,預設是放在web.xml同一層目錄。

<servlet>
<servlet-name>spring-ws</servlet-name>
<servlet-class>org.springframework.ws.transport.http.MessageDispatcherServlet</servlet-class>
<init-param>
<param-name>transformWsdlLocations</param-name>
<param-value>true</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>spring-ws</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>

在Servlet-class 中可加入 transformWsdlLocations 設定,代表在spring-ws-servlet.xml中的sws:dynamic-wsdl的locationUri屬性可以設置為相對路徑。
2. 設置spring-ws-servlet.xml
<context:component-scan base-package="com.test.ws"/>
<sws:annotation-driven/>

<sws:dynamic-wsdl id="patients" portTypeName="PatientsPort" locationUri="/service" targetNamespace="http://ws.test.com/om">
<sws:xsd location="classpath:resource/patients.xsd" />
</sws:dynamic-wsdl>

<oxm:jaxb2-marshaller id="jaxbMarshallerBean">
<oxm:class-to-be-bound name="com.test.ws.om.GetPatientRequest" />
</oxm:jaxb2-marshaller>

<sws:interceptors>
<bean class="org.springframework.ws.soap.security.wss4j.Wss4jSecurityInterceptor">
<property name="validationActions" value="Timestamp UsernameToken"/>
<property name="timestampStrict" value="true"/>
<property name="validationCallbackHandler" >
<bean class="org.springframework.ws.soap.security.wss4j.callback.SimplePasswordValidationCallbackHandler">
<property name="users">
<props>
<prop key="admin">passwd</prop>
</props>
</property>
</bean>
</property>
</bean>
</sws:interceptors>
3. 根據 Upto原則,設計resource/patients.xsd
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"
targetNamespace="http://ws.test.com/om" xmlns:tns="http://ws.test.com/om" >
<xs:element name="getPatientRequest">
<xs:complexType>
<xs:sequence>
<xs:element name="patientId" type="xs:int"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="getPatientResponse">
<xs:complexType>
<xs:sequence>
<xs:element name="patient" type="tns:patient"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="patient">
<xs:sequence>
<xs:element name="hisId" type="xs:int"/>
<xs:element name="name" type="xs:string"/>
<xs:element name="age" type="xs:int"/>
<xs:element name="class" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:schema>

4. 利用JAXB指令,產生傳送物件:
xjc.bat" -encoding UTF-8 -d C:/Temp/Test C:/Temp/Test/patients.xsd

5. 將產生的物件複製回Eclipse的目錄下。

6. 開始依據patients.xsd撰寫 Endpoint
@Endpoint
public class PatientEndpoint {

@PayloadRoot(namespace = “http://ws.test.com/om", localPart = “getPatientRequest")
@ResponsePayload
public GetPatientResponse getPatient(@RequestPayload GetPatientRequest request) {
GetPatientResponse response = new GetPatientResponse();
Patient p = new Patient();
p.setAge(1);
p.setHisId(321);
p.setName(“張三");
response.setPatient(p);
return response;
}
}

7. 利用SoapUI 來測試,看看是否能真的產生XML。

8. 撰寫Client端程式。

org.springframework.oxm.jaxb.Jaxb2Marshaller marshaller = new org.springframework.oxm.jaxb.Jaxb2Marshaller();
marshaller.setClassesToBeBound( new Class[]{com.test.ws.om.GetPatientRequest.class, com.test.ws.om.GetPatientResponse.class, com.test.ws.om.Patient.class} );

// Debug
// // com.test.ws.om.GetPatientRequest request = new com.test.ws.om.GetPatientRequest();
// // request.setPatientId(123);
// // marshaller.marshal(request, new javax.xml.transform.stream.StreamResult(System.out));
com.test.ws.om.GetPatientRequest request = (com.test.ws.om.GetPatientRequest) marshaller.unmarshal( new javax.xml.transform.stream.StreamSource( new java.io.StringReader(“<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><getPatientRequest xmlns=\"http://ws.test.com/om\"><patientId>123</patientId></getPatientRequest>")) );
org.springframework.ws.client.core.WebServiceTemplate template = new org.springframework.ws.client.core.WebServiceTemplate(marshaller);

Wss4jSecurityInterceptor wss4jSecurityInterceptor = new Wss4jSecurityInterceptor();
wss4jSecurityInterceptor.setSecurementActions(“Timestamp UsernameToken");
wss4jSecurityInterceptor.setSecurementUsername(“admin");
wss4jSecurityInterceptor.setSecurementPassword(“passwd");
wss4jSecurityInterceptor.setTimestampPrecisionInMilliseconds(true);

ClientInterceptor[] interceptors = new ClientInterceptor[] {wss4jSecurityInterceptor};
template.setInterceptors(interceptors);

com.test.ws.om.GetPatientResponse response = (com.test.ws.om.GetPatientResponse) template.marshalSendAndReceive(“http://localhost:8080/EMRWS/service/patients.wsdl", request);
// ,new org.springframework.ws.client.core.WebServiceMessageCallback(){
// @Override
// public void doWithMessage(WebServiceMessage arg0) throws IOException, TransformerException {
// System.out.println(“Hello");
// }
// });

System.out.println(response.getPatient().getName());

廣告

About fenjj

Perfect !!??...
本篇發表於 Uncategorized。將永久鏈結加入書籤。

發表迴響

在下方填入你的資料或按右方圖示以社群網站登入:

WordPress.com Logo

您的留言將使用 WordPress.com 帳號。 登出 / 變更 )

Twitter picture

您的留言將使用 Twitter 帳號。 登出 / 變更 )

Facebook照片

您的留言將使用 Facebook 帳號。 登出 / 變更 )

Google+ photo

您的留言將使用 Google+ 帳號。 登出 / 變更 )

連結到 %s