From 7d3b420ae98a6eb7521bdceb1b7fe7d14a44e140 Mon Sep 17 00:00:00 2001 From: Arjen Poutsma Date: Thu, 30 Jan 2014 16:13:42 +0100 Subject: [PATCH] Updated tutorial to SWS 2.1 Updated tutorial to use Spring-WS 2.0.1, specifically JDOM 2. Issue: SWS-854 --- src/reference/docbook/tutorial.xml | 103 +++++++++++++++++------------ 1 file changed, 61 insertions(+), 42 deletions(-) diff --git a/src/reference/docbook/tutorial.xml b/src/reference/docbook/tutorial.xml index 92fa9ef1..fb2aed04 100644 --- a/src/reference/docbook/tutorial.xml +++ b/src/reference/docbook/tutorial.xml @@ -539,21 +539,23 @@
Handling the XML Message - In this sample application, we are going to use JDom to handle + In this sample application, we are going to use JDom 2 to handle the XML message. We are also using XPath, because it allows us to select particular parts of the XML JDOM tree, without requiring strict schema conformance. - - - - + + + + startDateExpression; - private XPath endDateExpression; + private XPathExpression endDateExpression; - private XPath nameExpression; + private XPathExpression firstNameExpression; - private HumanResourceService humanResourceService; + private XPathExpression 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 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 + "]"); + } + } }]]> @@ -625,12 +634,14 @@ public class HolidayEndpoint { HumanResourceService business service to operate, so we inject the dependency via the constructor and annotate it with @Autowired. - Next, we set up XPath expressions using the JDOM API. - There are three expressions: //hr:StartDate for + + + Next, we set up XPath expressions using the JDOM2 API. + There are four expressions: //hr:StartDate for extracting the <StartDate> text value, //hr:EndDate for - extracting the end date and concat(//hr:FirstName,' ',//hr:LastName) - for extracting and concatenating the names of the employee. + extracting the end date and two for extracting the names of + the employee. @@ -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 HolidayRequest local part and the http://mycompany.com/hr/schemas namespace. + + More information about mapping messages to endpoints is provided in the next section. @@ -651,12 +664,18 @@ public class HolidayEndpoint { The @RequestPayload annotation indicates that the holidayRequest parameter should be mapped to the payload of the request message. + + We use the XPath expressions to extract the string values from the XML messages, and convert these values to Date objects using a - SimpleDateFormat. + SimpleDateFormat (the parseData method). + + 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. + + Finally, we define a void 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 { <dependency> <groupId>jdom</groupId> <artifactId>jdom</artifactId> - <version>1.0</version> + <version>2.0.1</version> </dependency> <dependency> <groupId>jaxen</groupId>