17 changed files with 537 additions and 24 deletions
@ -0,0 +1,14 @@
@@ -0,0 +1,14 @@
|
||||
== Spring Boot - Samples - Web Services |
||||
|
||||
This sample project demonstrates how to use http://projects.spring.io/spring-ws/[Spring Web Services] |
||||
with Spring Boot. It is an implementation of the |
||||
http://docs.spring.io/spring-ws/site/reference/html/tutorial.html#tutorial.implementing.endpoint[Holiday Request sample] |
||||
in the Spring Web Services reference guilde. |
||||
|
||||
The sample uses Maven. It can be built and run from the command line: |
||||
|
||||
---- |
||||
$ mvn spring-boot:run |
||||
---- |
||||
|
||||
http://localhost:8080/services/holidayService/holiday.wsdl will now display the generated WSDL. |
||||
@ -0,0 +1,52 @@
@@ -0,0 +1,52 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?> |
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
||||
<modelVersion>4.0.0</modelVersion> |
||||
<parent> |
||||
<!-- Your own application should inherit from spring-boot-starter-parent --> |
||||
<artifactId>spring-boot-samples</artifactId> |
||||
<groupId>org.springframework.boot</groupId> |
||||
<version>1.1.0.BUILD-SNAPSHOT</version> |
||||
</parent> |
||||
<artifactId>spring-boot-sample-ws</artifactId> |
||||
<name>Spring Boot Web Services Sample</name> |
||||
<description>Spring Boot Web Services Sample</description> |
||||
<url>http://projects.spring.io/spring-boot/</url> |
||||
<organization> |
||||
<name>Pivotal Software, Inc.</name> |
||||
<url>http://www.spring.io</url> |
||||
</organization> |
||||
<properties> |
||||
<main.basedir>${basedir}/../..</main.basedir> |
||||
</properties> |
||||
<dependencies> |
||||
<dependency> |
||||
<groupId>org.springframework.boot</groupId> |
||||
<artifactId>spring-boot-starter-test</artifactId> |
||||
<scope>test</scope> |
||||
</dependency> |
||||
<dependency> |
||||
<groupId>org.springframework.boot</groupId> |
||||
<artifactId>spring-boot-starter-ws</artifactId> |
||||
</dependency> |
||||
<dependency> |
||||
<groupId>jaxen</groupId> |
||||
<artifactId>jaxen</artifactId> |
||||
</dependency> |
||||
<dependency> |
||||
<groupId>org.jdom</groupId> |
||||
<artifactId>jdom2</artifactId> |
||||
</dependency> |
||||
<dependency> |
||||
<groupId>wsdl4j</groupId> |
||||
<artifactId>wsdl4j</artifactId> |
||||
</dependency> |
||||
</dependencies> |
||||
<build> |
||||
<plugins> |
||||
<plugin> |
||||
<groupId>org.springframework.boot</groupId> |
||||
<artifactId>spring-boot-maven-plugin</artifactId> |
||||
</plugin> |
||||
</plugins> |
||||
</build> |
||||
</project> |
||||
@ -0,0 +1,31 @@
@@ -0,0 +1,31 @@
|
||||
/* |
||||
* Copyright 2012-2014 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
package sample.ws; |
||||
|
||||
import org.springframework.boot.SpringApplication; |
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; |
||||
import org.springframework.context.annotation.ComponentScan; |
||||
import org.springframework.context.annotation.Configuration; |
||||
|
||||
@Configuration |
||||
@EnableAutoConfiguration |
||||
@ComponentScan |
||||
public class SampleWsApplication { |
||||
|
||||
public static void main(String[] args) throws Exception { |
||||
SpringApplication.run(SampleWsApplication.class, args); |
||||
} |
||||
} |
||||
@ -0,0 +1,55 @@
@@ -0,0 +1,55 @@
|
||||
/* |
||||
* Copyright 2012-2014 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
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; |
||||
|
||||
@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")); |
||||
} |
||||
} |
||||
@ -0,0 +1,79 @@
@@ -0,0 +1,79 @@
|
||||
/* |
||||
* Copyright 2012-2014 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
package sample.ws.endpoint; |
||||
|
||||
import java.text.SimpleDateFormat; |
||||
import java.util.Date; |
||||
|
||||
import javax.xml.xpath.XPathExpressionException; |
||||
import javax.xml.xpath.XPathFactoryConfigurationException; |
||||
|
||||
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; |
||||
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; |
||||
|
||||
@Endpoint |
||||
public class HolidayEndpoint { |
||||
|
||||
private static final String NAMESPACE_URI = "http://mycompany.com/hr/schemas"; |
||||
|
||||
private XPathExpression<Element> startDateExpression; |
||||
private XPathExpression<Element> endDateExpression; |
||||
private XPathExpression<String> nameExpression; |
||||
|
||||
private HumanResourceService humanResourceService; |
||||
|
||||
@Autowired |
||||
public HolidayEndpoint(HumanResourceService humanResourceService) |
||||
throws JDOMException, XPathFactoryConfigurationException, |
||||
XPathExpressionException { |
||||
this.humanResourceService = humanResourceService; |
||||
|
||||
Namespace namespace = Namespace.getNamespace("hr", NAMESPACE_URI); |
||||
|
||||
XPathFactory xPathFactory = XPathFactory.instance(); |
||||
|
||||
this.startDateExpression = xPathFactory.compile("//hr:StartDate", |
||||
Filters.element(), null, namespace); |
||||
this.endDateExpression = xPathFactory.compile("//hr:EndDate", Filters.element(), |
||||
null, namespace); |
||||
this.nameExpression = xPathFactory.compile( |
||||
"concat(//hr:FirstName,' ',//hr:LastName)", Filters.fstring(), null, |
||||
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(this.startDateExpression.evaluateFirst( |
||||
holidayRequest).getText()); |
||||
Date endDate = dateFormat.parse(this.endDateExpression.evaluateFirst( |
||||
holidayRequest).getText()); |
||||
String name = this.nameExpression.evaluateFirst(holidayRequest); |
||||
|
||||
this.humanResourceService.bookHoliday(startDate, endDate, name); |
||||
} |
||||
} |
||||
@ -0,0 +1,23 @@
@@ -0,0 +1,23 @@
|
||||
/* |
||||
* Copyright 2012-2014 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
package sample.ws.service; |
||||
|
||||
import java.util.Date; |
||||
|
||||
public interface HumanResourceService { |
||||
|
||||
void bookHoliday(Date startDate, Date endDate, String name); |
||||
} |
||||
@ -0,0 +1,34 @@
@@ -0,0 +1,34 @@
|
||||
/* |
||||
* Copyright 2012-2014 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
package sample.ws.service; |
||||
|
||||
import java.util.Date; |
||||
|
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
import org.springframework.stereotype.Service; |
||||
|
||||
@Service |
||||
public class StubHumanResourceService implements HumanResourceService { |
||||
|
||||
private final Logger logger = LoggerFactory.getLogger(StubHumanResourceService.class); |
||||
|
||||
@Override |
||||
public void bookHoliday(Date startDate, Date endDate, String name) { |
||||
this.logger.info("Booking holiday for [{} - {}] for [{}] ", startDate, endDate, |
||||
name); |
||||
} |
||||
} |
||||
@ -0,0 +1,26 @@
@@ -0,0 +1,26 @@
|
||||
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" |
||||
xmlns:hr="http://mycompany.com/hr/schemas" |
||||
elementFormDefault="qualified" |
||||
targetNamespace="http://mycompany.com/hr/schemas"> |
||||
<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> |
||||
@ -0,0 +1,69 @@
@@ -0,0 +1,69 @@
|
||||
/* |
||||
* Copyright 2012-2014 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
package sample.ws; |
||||
|
||||
import java.io.StringReader; |
||||
|
||||
import javax.xml.transform.stream.StreamResult; |
||||
import javax.xml.transform.stream.StreamSource; |
||||
|
||||
import org.junit.Before; |
||||
import org.junit.Test; |
||||
import org.junit.runner.RunWith; |
||||
import org.springframework.beans.factory.annotation.Value; |
||||
import org.springframework.boot.test.IntegrationTest; |
||||
import org.springframework.boot.test.SpringApplicationConfiguration; |
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
||||
import org.springframework.test.context.web.WebAppConfiguration; |
||||
import org.springframework.ws.client.core.WebServiceTemplate; |
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class) |
||||
@SpringApplicationConfiguration(classes = SampleWsApplication.class) |
||||
@WebAppConfiguration |
||||
@IntegrationTest |
||||
public class SampleWsApplicationTests { |
||||
|
||||
private WebServiceTemplate webServiceTemplate = new WebServiceTemplate(); |
||||
|
||||
@Value("${local.server.port}") |
||||
private int serverPort; |
||||
|
||||
@Before |
||||
public void setUp() { |
||||
this.webServiceTemplate.setDefaultUri("http://localhost:" + this.serverPort |
||||
+ "/services/"); |
||||
} |
||||
|
||||
@Test |
||||
public void testSendingHolidayRequest() { |
||||
final String request = "<hr:HolidayRequest xmlns:hr=\"http://mycompany.com/hr/schemas\">" |
||||
+ " <hr:Holiday>" |
||||
+ " <hr:StartDate>2013-10-20</hr:StartDate>" |
||||
+ " <hr:EndDate>2013-11-22</hr:EndDate>" |
||||
+ " </hr:Holiday>" |
||||
+ " <hr:Employee>" |
||||
+ " <hr:Number>1</hr:Number>" |
||||
+ " <hr:FirstName>John</hr:FirstName>" |
||||
+ " <hr:LastName>Doe</hr:LastName>" |
||||
+ " </hr:Employee>" |
||||
+ "</hr:HolidayRequest>"; |
||||
|
||||
StreamSource source = new StreamSource(new StringReader(request)); |
||||
StreamResult result = new StreamResult(System.out); |
||||
|
||||
this.webServiceTemplate.sendSourceAndReceiveToResult(source, result); |
||||
} |
||||
} |
||||
@ -0,0 +1,50 @@
@@ -0,0 +1,50 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?> |
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" |
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
||||
<modelVersion>4.0.0</modelVersion> |
||||
<parent> |
||||
<groupId>org.springframework.boot</groupId> |
||||
<artifactId>spring-boot-starters</artifactId> |
||||
<version>1.1.0.BUILD-SNAPSHOT</version> |
||||
</parent> |
||||
<artifactId>spring-boot-starter-ws</artifactId> |
||||
<name>Spring Boot Web Services Starter</name> |
||||
<description>Spring Boot Web Services Starter</description> |
||||
<url>http://projects.spring.io/spring-boot/</url> |
||||
<organization> |
||||
<name>Pivotal Software, Inc.</name> |
||||
<url>http://www.spring.io</url> |
||||
</organization> |
||||
<properties> |
||||
<main.basedir>${basedir}/../..</main.basedir> |
||||
</properties> |
||||
<dependencies> |
||||
<dependency> |
||||
<groupId>org.springframework.boot</groupId> |
||||
<artifactId>spring-boot-starter</artifactId> |
||||
</dependency> |
||||
<dependency> |
||||
<groupId>org.springframework.boot</groupId> |
||||
<artifactId>spring-boot-starter-web</artifactId> |
||||
</dependency> |
||||
<dependency> |
||||
<groupId>org.springframework</groupId> |
||||
<artifactId>spring-core</artifactId> |
||||
<exclusions> |
||||
<exclusion> |
||||
<groupId>commons-logging</groupId> |
||||
<artifactId>commons-logging</artifactId> |
||||
</exclusion> |
||||
</exclusions> |
||||
</dependency> |
||||
<dependency> |
||||
<groupId>org.springframework.ws</groupId> |
||||
<artifactId>spring-ws-core</artifactId> |
||||
</dependency> |
||||
<dependency> |
||||
<groupId>org.springframework.ws</groupId> |
||||
<artifactId>spring-ws-support</artifactId> |
||||
</dependency> |
||||
</dependencies> |
||||
</project> |
||||
@ -0,0 +1 @@
@@ -0,0 +1 @@
|
||||
provides: spring-ws-core,spring-ws-support |
||||
Loading…
Reference in new issue