Spring Web Services Starter

- upgraded Spring WS to 2.2.0.RELEASE
- replaced default MVC DispatcherServlet with MessageDispatcherServlet
- migrated XML based config with nww Spring WS Java config

Fixes: gh-412
This commit is contained in:
Maciej Walkowiak
2014-06-06 15:37:30 +02:00
committed by Andy Wilkinson
parent e2a449da97
commit 95a6ce9e48
10 changed files with 191 additions and 183 deletions

View File

@@ -2,12 +2,8 @@ package sample.ws;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.context.embedded.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.ws.transport.http.MessageDispatcherServlet;
/**
* Created by in329dei on 28-2-14.
@@ -15,27 +11,9 @@ import org.springframework.ws.transport.http.MessageDispatcherServlet;
@Configuration
@EnableAutoConfiguration
@ComponentScan
@ImportResource("classpath:/META-INF/spring/spring-ws-context.xml")
public class SampleWsApplication {
public static void main(String[] args) throws Exception {
SpringApplication.run(SampleWsApplication.class, args);
}
@Bean
public ServletRegistrationBean messageDispatcherServletRegistration() {
MessageDispatcherServlet mds = new MessageDispatcherServlet();
mds.setTransformWsdlLocations(true);
ServletRegistrationBean srb = new ServletRegistrationBean(messageDispatcherServlet(), "/services/*");
srb.setLoadOnStartup(1);
return srb;
}
@Bean
public MessageDispatcherServlet messageDispatcherServlet() {
MessageDispatcherServlet mds = new MessageDispatcherServlet();
mds.setTransformWsdlLocations(true);
return mds;
}
public static void main(String[] args) throws Exception {
SpringApplication.run(SampleWsApplication.class, args);
}
}

View File

@@ -0,0 +1,45 @@
package sample.ws;
import org.springframework.boot.context.embedded.ServletRegistrationBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.ws.config.annotation.EnableWs;
import org.springframework.ws.config.annotation.WsConfigurerAdapter;
import org.springframework.ws.transport.http.MessageDispatcherServlet;
import org.springframework.ws.wsdl.wsdl11.DefaultWsdl11Definition;
import org.springframework.xml.xsd.SimpleXsdSchema;
import org.springframework.xml.xsd.XsdSchema;
/**
* Configures Spring Web Service components
*
* @author Maciej Walkowiak
*/
@EnableWs
@Configuration
public class WebServiceConfig extends WsConfigurerAdapter {
@Bean
public ServletRegistrationBean dispatcherServlet(ApplicationContext applicationContext) {
MessageDispatcherServlet servlet = new MessageDispatcherServlet();
servlet.setApplicationContext(applicationContext);
return new ServletRegistrationBean(servlet, "/services/*");
}
@Bean(name = "holiday")
public DefaultWsdl11Definition defaultWsdl11Definition(XsdSchema countriesSchema) {
DefaultWsdl11Definition wsdl11Definition = new DefaultWsdl11Definition();
wsdl11Definition.setPortTypeName("HumanResource");
wsdl11Definition.setLocationUri("/holidayService/");
wsdl11Definition.setTargetNamespace("http://mycompany.com/hr/definitions");
wsdl11Definition.setSchema(countriesSchema);
return wsdl11Definition;
}
@Bean
public XsdSchema countriesSchema() {
return new SimpleXsdSchema(new ClassPathResource("META-INF/schemas/hr.xsd"));
}
}

View File

@@ -3,56 +3,55 @@ package sample.ws.endpoint;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.Namespace;
import org.jdom2.xpath.XPath;
import org.jdom2.filter.Filters;
import org.jdom2.xpath.XPathExpression;
import org.jdom2.xpath.XPathFactory;
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 sample.ws.service.HumanResourceService;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactoryConfigurationException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* Created by in329dei on 28-2-14.
* @author in329dei
* @author Maciej Walkowiak
*/
@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 XPathExpression<Element> endDateExpression;
private XPathExpression<String> nameExpression;
private XPath endDateExpression;
private HumanResourceService humanResourceService;
private XPath nameExpression;
@Autowired
public HolidayEndpoint(HumanResourceService humanResourceService) throws JDOMException, XPathFactoryConfigurationException, XPathExpressionException {
this.humanResourceService = humanResourceService;
private HumanResourceService humanResourceService;
Namespace namespace = Namespace.getNamespace("hr", NAMESPACE_URI);
@Autowired
public HolidayEndpoint(HumanResourceService humanResourceService) throws JDOMException
{
this.humanResourceService = humanResourceService;
XPathFactory xPathFactory = XPathFactory.instance();
Namespace namespace = Namespace.getNamespace("hr", NAMESPACE_URI);
startDateExpression = xPathFactory.compile("//hr:StartDate", Filters.element(), null, namespace);
endDateExpression = xPathFactory.compile("//hr:EndDate", Filters.element(), null, namespace);
nameExpression = xPathFactory.compile("concat(//hr:FirstName,' ',//hr:LastName)", Filters.fstring(), null, namespace);
}
startDateExpression = XPath.newInstance("//hr:StartDate");
startDateExpression.addNamespace(namespace);
@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.evaluateFirst(holidayRequest).getText());
Date endDate = dateFormat.parse(endDateExpression.evaluateFirst(holidayRequest).getText());
String name = nameExpression.evaluateFirst(holidayRequest);
endDateExpression = XPath.newInstance("//hr:EndDate");
endDateExpression.addNamespace(namespace);
nameExpression = XPath.newInstance("concat(//hr:FirstName,' ',//hr:LastName)");
nameExpression.addNamespace(namespace);
}
@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);
}
humanResourceService.bookHoliday(startDate, endDate, name);
}
}

View File

@@ -6,5 +6,5 @@ import java.util.Date;
* Created by in329dei on 28-2-14.
*/
public interface HumanResourceService {
void bookHoliday(Date startDate, Date endDate, String name);
void bookHoliday(Date startDate, Date endDate, String name);
}

View File

@@ -11,11 +11,10 @@ import java.util.Date;
*/
@Service
public class StubHumanResourceService implements HumanResourceService {
private final Logger logger = LoggerFactory.getLogger(StubHumanResourceService.class);
private final Logger logger = LoggerFactory.getLogger(StubHumanResourceService.class);
@Override
public void bookHoliday(Date startDate, Date endDate, String name) {
logger.info("Booking holiday for [{} - {}] for [{}] ", startDate, endDate, name);
}
@Override
public void bookHoliday(Date startDate, Date endDate, String name) {
logger.info("Booking holiday for [{} - {}] for [{}] ", startDate, endDate, name);
}
}