TechnoBuzz

A Techno Blog, mainly about Java

Spring Web Services

Contract First Web Services tutorial: When using contract-first, you start with the WSDL contract, and use Java to implement said contract.  Spring-WS only supports the contract-first development style.

3.5. Creating the project

src/main/webapp/WEB-INF/

spring-ws-servlet.html

<?xml version=”1.0″ encoding=”UTF-8″?>
<beans xmlns=”http://www.springframework.org/schema/beans&#8221;
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance&#8221;
xmlns:context=”http://www.springframework.org/schema/context&#8221;
xmlns:sws=”http://www.springframework.org/schema/web-services&#8221;
xsi:schemaLocation=”http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/web-services http://www.springframework.org/schema/web-services/web-services-2.0.xsd”&gt;

<sws:annotation-driven/>
</beans>

a) add this line to above file <context:component-scan base-package=”com.mycompany.hr”/>

b) Create java package com.mycompany.hr.ws

web.xml:

<?xml version=”1.0″ encoding=”UTF-8″?>
<web-app xmlns=”http://java.sun.com/xml/ns/j2ee&#8221; xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance&#8221;
xsi:schemaLocation=”http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd&#8221;
version=”2.4″>
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>spring-ws</servlet-name>
<servlet-class>org.springframework.ws.transport.http.MessageDispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>spring-ws</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>

Spring-WS, you will implement Endpoints to handle incoming XML messages. An endpoint is typically created by annotating a class with the @Endpoint annotation.

you will create one or more methods that handle incoming request.

3.6.1. Handling the XML Message

use JDom to handle the XML message. We are also using XPath, because it allows us to select particular parts of the XML JDOM tree,

New class HolidayEndpoint

package com.mycompany.hr.ws;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ws.server.endpoint.annotation.Endpoint;
import org.springframework.ws.server.endpoint.annotation.PayloadRoot;
import org.springframework.ws.server.endpoint.annotation.RequestPayload;
import com.mycompany.hr.service.HumanResourceService;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.Namespace;
import org.jdom.xpath.XPath;

@Endpoint                                                                                
public class HolidayEndpoint {

private static final String NAMESPACE_URI = “http://mycompany.com/hr/schemas&#8221;;

//The HolidayEndpoint requires the HumanResourceService business service to operate, so we inject the dependency via the constructor and annotate it with @Autowired

@Autowired
public HolidayEndpoint(HumanResourceService humanResourceService)
throws JDOMException {
this.humanResourceService = humanResourceService;
Namespace namespace = Namespace.getNamespace(“hr”, NAMESPACE_URI);
startDateExpression = XPath.newInstance(“//hr:StartDate”);
startDateExpression.addNamespace(namespace);
endDateExpression = XPath.newInstance(“//hr:EndDate”);
endDateExpression.addNamespace(namespace);
nameExpression = XPath.newInstance(“concat(//hr:FirstName,’ ‘,//hr:LastName)”);
nameExpression.addNamespace(namespace);
}

//The @PayloadRoot annotation tells Spring-WS that the handleHolidayRequest method is suitable for handling XML messages.
//In this case, it can handle XML elements that have the HolidayRequest local part and the http://mycompany.com/hr/schemas namespace
//gets passed with the <HolidayRequest/> element from the incoming XML message.
// The @RequestPayload annotation indicates that the holidayRequest parameter should be mapped to the payload of the request message

@PayloadRoot(namespace = NAMESPACE_URI, localPart = “HolidayRequest”)         
public void handleHolidayRequest(@RequestPayload Element holidayRequest)
throws Exception {
SimpleDateFormat dateFormat = new SimpleDateFormat(“yyyy-MM-dd”);
Date startDate = dateFormat.parse(startDateExpression.valueOf(holidayRequest));
Date endDate = dateFormat.parse(endDateExpression.valueOf(holidayRequest));
String name = nameExpression.valueOf(holidayRequest);
humanResourceService.bookHoliday(startDate, endDate, name);
}

We skipped the Schema and WSDL, but they follow here (Note that Spring WS will create the WSDL, but here it is for getting to know it):

Schema………………………………………………………………………………………..
<xs:schema xmlns:xs=”http://www.w3.org/2001/XMLSchema&#8221;
xmlns:hr=”http://mycompany.com/hr/schemas&#8221;
elementFormDefault=”qualified”
targetNamespace=”http://mycompany.com/hr/schemas”&gt;
<xs:element name=”HolidayRequest”>
<xs:complexType>
<xs:all>
<xs:element name=”Holiday” type=”hr:HolidayType”/>
<xs:element name=”Employee” type=”hr:EmployeeType”/>
</xs:all>
</xs:complexType>
</xs:element>
<xs:complexType name=”HolidayType”>
<xs:sequence>
<xs:element name=”StartDate” type=”xs:date”/>
<xs:element name=”EndDate” type=”xs:date”/>
</xs:sequence>
</xs:complexType>
<xs:complexType name=”EmployeeType”>
<xs:sequence>
<xs:element name=”Number” type=”xs:integer”/>
<xs:element name=”FirstName” type=”xs:string”/>
<xs:element name=”LastName” type=”xs:string”/>
</xs:sequence>
</xs:complexType>
</xs:schema>

Note: Save this schema as hr.xsd in WEB-INF

WSDL………………………………………………………………………..
<wsdl:definitions xmlns:wsdl=”http://schemas.xmlsoap.org/wsdl/&#8221;
xmlns:soap=”http://schemas.xmlsoap.org/wsdl/soap/&#8221;
xmlns:schema=”http://mycompany.com/hr/schemas&#8221;
xmlns:tns=”http://mycompany.com/hr/definitions&#8221;
targetNamespace=”http://mycompany.com/hr/definitions”&gt;
<!–SCHEMA REFERENCE–>
<wsdl:types>
<xsd:schema xmlns:xsd=”http://www.w3.org/2001/XMLSchema”&gt;
<xsd:import namespace=”http://mycompany.com/hr/schemas&#8221;

schemaLocation=”hr.xsd”/>
</xsd:schema>
</wsdl:types>

<!–MESSAGE HolidayRequest–>
<wsdl:message name=”HolidayRequest”>
<wsdl:part element=”schema:HolidayRequest” name=”HolidayRequest”/>
</wsdl:message>
<!–MESSAGE Holiday Request to port type Human Resource as operation–>
<wsdl:portType name=”HumanResource”>
<wsdl:operation name=”Holiday”>
<wsdl:input message=”tns:HolidayRequest” name=”HolidayRequest”/>

                  </wsdl:operation>

       </wsdl:portType>

<!-Binding which tells the client how to invoke the operations just defined and service which tells where to invoke it->

<wsdl:binding name=”HumanResourceBinding” type=”tns:HumanResource”>
<soap:binding style=”document”
transport=”http://schemas.xmlsoap.org/soap/http”/&gt;
<wsdl:operation name=”Holiday”>
<soap:operation soapAction=”http://mycompany.com/RequestHoliday”/&gt;
<wsdl:input name=”HolidayRequest”>
<soap:body use=”literal”/>
</wsdl:input>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name=”HumanResourceService”>
<wsdl:port binding=”tns:HumanResourceBinding” name=”HumanResourcePort”>

<soap:address location=”http://localhost:8080/holidayService/”/&gt;
</wsdl:port>
</wsdl:service>

</wsdl:definitions>

To generate the WSDL by spring ws add the following to spring-ws-servlet.html:

<sws:dynamic-wsdl id=”holiday”
portTypeName=”HumanResource”
locationUri=”/holidayService/”
targetNamespace=”http://mycompany.com/hr/definitions”&gt;
<sws:xsd location=”/WEB-INF/hr.xsd”/>
</sws:dynamic-wsdl>

The web.xml file is modified as follows:

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

http://localhost:8080/holidayService/holiday.wsdl

http://static.springsource.org/spring-ws/sites/2.0/faq.html

http://www.springbyexample.org/examples/simple-spring-web-services.html

Spring WS+JAXB

Reference to JAXB Plugin

http://aigloss.blogspot.com/2011/07/creating-spring-web-services-with.html?m=1

http://krams915.blogspot.com/2010/12/spring-ws-2-and-spring-3-mvc.html?m=1

http://javaclue.blogspot.com/2012/05/soap-web-service-using-spring-ws-20.html?m=1

http://www.disasterarea.co.uk/blog/?tag=spring-ws

Advertisements

September 19, 2012 - Posted by | Uncategorized

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: