Updated tutorial to SWS 2.1

Updated tutorial to use Spring-WS 2.0.1, specifically JDOM 2.

Issue: SWS-854
This commit is contained in:
Arjen Poutsma
2014-01-30 16:13:42 +01:00
parent cca13ec49b
commit 7d3b420ae9

View File

@@ -539,21 +539,23 @@
<section>
<title>Handling the XML Message</title>
<para>
In this sample application, we are going to use <ulink url="http://www.jdom.org">JDom</ulink> to handle
In this sample application, we are going to use <ulink url="http://www.jdom.org">JDom 2</ulink> to handle
the XML message.
We are also using <ulink url="http://www.w3schools.com/xpath/">XPath</ulink>, because it allows us to
select particular parts of the XML JDOM tree, without requiring strict schema conformance.
</para>
<programlistingco>
<areaspec>
<area id="tutorial.endpoint.atEndpoint" coords="17"/>
<area id="tutorial.endpoint.constr" coords="31"/>
<area id="tutorial.endpoint.payloadRoot" coords="47"/>
<area id="tutorial.endpoint.method" coords="48"/>
<area id="tutorial.endpoint.atEndpoint" coords="21"/>
<area id="tutorial.endpoint.constr" coords="35"/>
<area id="tutorial.endpoint.payloadRoot" coords="48"/>
<area id="tutorial.endpoint.method" coords="49"/>
</areaspec>
<programlisting><![CDATA[package com.mycompany.hr.ws;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import org.springframework.beans.factory.annotation.Autowired;
@@ -562,51 +564,58 @@ 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;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.Namespace;
import org.jdom2.filter.Filters;
import org.jdom2.xpath.XPathExpression;
import org.jdom2.xpath.XPathFactory;
@Endpoint
public class HolidayEndpoint {
private static final String NAMESPACE_URI = "http://mycompany.com/hr/schemas";
private static final String NAMESPACE_URI = "http://mycompany.com/hr/schemas";
private XPath startDateExpression;
private XPathExpression<Element> startDateExpression;
private XPath endDateExpression;
private XPathExpression<Element> endDateExpression;
private XPath nameExpression;
private XPathExpression<Element> firstNameExpression;
private HumanResourceService humanResourceService;
private XPathExpression<Element> lastNameExpression;
@Autowired
public HolidayEndpoint(HumanResourceService humanResourceService)
throws JDOMException {
this.humanResourceService = humanResourceService;
private HumanResourceService humanResourceService;
Namespace namespace = Namespace.getNamespace("hr", NAMESPACE_URI);
@Autowired
public HolidayEndpoint(HumanResourceService humanResourceService) throws JDOMException {
this.humanResourceService = humanResourceService;
startDateExpression = XPath.newInstance("//hr:StartDate");
startDateExpression.addNamespace(namespace);
Namespace namespace = Namespace.getNamespace("hr", NAMESPACE_URI);
XPathFactory xPathFactory = XPathFactory.instance();
startDateExpression = xPathFactory.compile("//hr:StartDate", Filters.element(), null, namespace);
endDateExpression = xPathFactory.compile("//hr:EndDate", Filters.element(), null, namespace);
firstNameExpression = xPathFactory.compile("//hr:FirstName", Filters.element(), null, namespace);
lastNameExpression = xPathFactory.compile("//hr:LastName", Filters.element(), null, namespace);
}
endDateExpression = XPath.newInstance("//hr:EndDate");
endDateExpression.addNamespace(namespace);
@PayloadRoot(namespace = NAMESPACE_URI, localPart = "HolidayRequest")
public void handleHolidayRequest(@RequestPayload Element holidayRequest) throws Exception {
Date startDate = parseDate(startDateExpression, holidayRequest);
Date endDate = parseDate(endDateExpression, holidayRequest);
String name = firstNameExpression.evaluateFirst(holidayRequest).getText() + " " + lastNameExpression.evaluateFirst(holidayRequest).getText();
nameExpression = XPath.newInstance("concat(//hr:FirstName,' ',//hr:LastName)");
nameExpression.addNamespace(namespace);
}
humanResourceService.bookHoliday(startDate, endDate, name);
}
@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);
}
private Date parseDate(XPathExpression<Element> expression, Element element) throws ParseException {
Element result = expression.evaluateFirst(element);
if (result != null) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
return dateFormat.parse(result.getText());
} else {
throw new IllegalArgumentException("Could not evaluate [" + expression + "] on [" + element + "]");
}
}
}]]></programlisting>
<calloutlist>
@@ -625,12 +634,14 @@ public class HolidayEndpoint {
<interfacename>HumanResourceService</interfacename> business service to operate, so we
inject the dependency via the constructor and annotate it with
<interfacename>@Autowired</interfacename>.
Next, we set up XPath expressions using the JDOM API.
There are three expressions: <literal>//hr:StartDate</literal> for
</para>
<para>
Next, we set up XPath expressions using the JDOM2 API.
There are four expressions: <literal>//hr:StartDate</literal> for
extracting the <literal>&lt;StartDate&gt;</literal> text value,
<literal>//hr:EndDate</literal> for
extracting the end date and <literal>concat(//hr:FirstName,' ',//hr:LastName)</literal>
for extracting and concatenating the names of the employee.
extracting the end date and two for extracting the names of
the employee.
</para>
</callout>
<callout arearefs="tutorial.endpoint.payloadRoot">
@@ -640,6 +651,8 @@ public class HolidayEndpoint {
The sort of message that this method can handle is indicated by the annotation values,
in this case, it can handle XML elements that have the <literal>HolidayRequest</literal>
local part and the <literal>http://mycompany.com/hr/schemas</literal> namespace.
</para>
<para>
More information about mapping messages to endpoints is provided in the next section.
</para>
</callout>
@@ -651,12 +664,18 @@ public class HolidayEndpoint {
The <interfacename>@RequestPayload</interfacename> annotation indicates that the
<parameter>holidayRequest</parameter> parameter should be mapped to the payload of the
request message.
</para>
<para>
We use the XPath expressions to extract the string values from the XML messages,
and convert these values to <classname>Date</classname> objects using a
<classname>SimpleDateFormat</classname>.
<classname>SimpleDateFormat</classname> (the <methodname>parseData</methodname> method).
</para>
<para>
With these values, we invoke a method on the business service.
Typically, this will result in a database transaction being started, and some records being
altered in the database.
</para>
<para>
Finally, we define a <literal>void</literal> return type, which indicates to Spring-WS
that we do not want to send a response message.
If we wanted a response message, we could have returned a JDOM Element
@@ -687,7 +706,7 @@ public class HolidayEndpoint {
&lt;dependency&gt;
&lt;groupId&gt;jdom&lt;/groupId&gt;
&lt;artifactId&gt;jdom&lt;/artifactId&gt;
&lt;version&gt;1.0&lt;/version&gt;
&lt;version&gt;2.0.1&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;jaxen&lt;/groupId&gt;