Skip to content

Commit 4e85906

Browse files
authored
Merge pull request eugenp#6218 from alv21/spring-soap
BAEL-2466 Spring soap
2 parents cc49ae8 + 29e4923 commit 4e85906

10 files changed

Lines changed: 300 additions & 0 deletions

File tree

pom.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,7 @@
750750
<module>spring-security-x509</module>
751751
<module>spring-session</module>
752752
<module>spring-sleuth</module>
753+
<module>spring-soap</module>
753754
<module>spring-social-login</module>
754755
<module>spring-spel</module>
755756
<module>spring-state-machine</module>
@@ -1460,6 +1461,7 @@
14601461
<module>spring-security-x509</module>
14611462
<module>spring-session</module>
14621463
<module>spring-sleuth</module>
1464+
<module>spring-soap</module>
14631465
<module>spring-social-login</module>
14641466
<module>spring-spel</module>
14651467
<module>spring-state-machine</module>

spring-soap/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/target/

spring-soap/pom.xml

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>com.baeldung</groupId>
7+
<artifactId>spring-soap</artifactId>
8+
<version>1.0.0</version>
9+
10+
<parent>
11+
<groupId>org.springframework.boot</groupId>
12+
<artifactId>spring-boot-starter-parent</artifactId>
13+
<version>2.1.2.RELEASE</version>
14+
</parent>
15+
16+
<properties>
17+
<java.version>1.8</java.version>
18+
</properties>
19+
20+
<dependencies>
21+
<!-- tag::springws[] -->
22+
<dependency>
23+
<groupId>org.springframework.boot</groupId>
24+
<artifactId>spring-boot-starter-web-services</artifactId>
25+
</dependency>
26+
<dependency>
27+
<groupId>wsdl4j</groupId>
28+
<artifactId>wsdl4j</artifactId>
29+
</dependency>
30+
<!-- end::springws[] -->
31+
<dependency>
32+
<groupId>org.springframework.boot</groupId>
33+
<artifactId>spring-boot-starter-test</artifactId>
34+
<scope>test</scope>
35+
</dependency>
36+
</dependencies>
37+
38+
<build>
39+
<plugins>
40+
<plugin>
41+
<groupId>org.springframework.boot</groupId>
42+
<artifactId>spring-boot-maven-plugin</artifactId>
43+
</plugin>
44+
45+
<!-- tag::xsd[] -->
46+
<plugin>
47+
<groupId>org.codehaus.mojo</groupId>
48+
<artifactId>jaxb2-maven-plugin</artifactId>
49+
<version>1.6</version>
50+
<executions>
51+
<execution>
52+
<id>xjc</id>
53+
<goals>
54+
<goal>xjc</goal>
55+
</goals>
56+
</execution>
57+
</executions>
58+
<configuration>
59+
<schemaDirectory>${project.basedir}/src/main/resources/</schemaDirectory>
60+
<outputDirectory>${project.basedir}/src/main/java</outputDirectory>
61+
<clearOutputDir>false</clearOutputDir>
62+
</configuration>
63+
</plugin>
64+
<!-- end::xsd[] -->
65+
</plugins>
66+
</build>
67+
68+
</project>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.baeldung.springsoap;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class Application {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(Application.class, args);
11+
}
12+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.baeldung.springsoap;
2+
3+
import com.baeldung.springsoap.gen.*;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.ws.server.endpoint.annotation.Endpoint;
6+
import org.springframework.ws.server.endpoint.annotation.PayloadRoot;
7+
import org.springframework.ws.server.endpoint.annotation.RequestPayload;
8+
import org.springframework.ws.server.endpoint.annotation.ResponsePayload;
9+
10+
@Endpoint
11+
public class CountryEndpoint {
12+
13+
private static final String NAMESPACE_URI = "http://www.baeldung.com/springsoap/gen";
14+
15+
private CountryRepository countryRepository;
16+
17+
@Autowired
18+
public CountryEndpoint(CountryRepository countryRepository) {
19+
this.countryRepository = countryRepository;
20+
}
21+
22+
@PayloadRoot(namespace = NAMESPACE_URI, localPart = "getCountryRequest")
23+
@ResponsePayload
24+
public GetCountryResponse getCountry(@RequestPayload GetCountryRequest request) {
25+
GetCountryResponse response = new GetCountryResponse();
26+
response.setCountry(countryRepository.findCountry(request.getName()));
27+
28+
return response;
29+
}
30+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.baeldung.springsoap;
2+
3+
import com.baeldung.springsoap.gen.*;
4+
import org.springframework.stereotype.Component;
5+
import org.springframework.util.Assert;
6+
7+
import javax.annotation.PostConstruct;
8+
import java.util.HashMap;
9+
import java.util.Map;
10+
11+
@Component
12+
public class CountryRepository {
13+
14+
private static final Map<String, Country> countries = new HashMap<>();
15+
16+
@PostConstruct
17+
public void initData() {
18+
Country spain = new Country();
19+
spain.setName("Spain");
20+
spain.setCapital("Madrid");
21+
spain.setCurrency(Currency.EUR);
22+
spain.setPopulation(46704314);
23+
24+
countries.put(spain.getName(), spain);
25+
26+
Country poland = new Country();
27+
poland.setName("Poland");
28+
poland.setCapital("Warsaw");
29+
poland.setCurrency(Currency.PLN);
30+
poland.setPopulation(38186860);
31+
32+
countries.put(poland.getName(), poland);
33+
34+
Country uk = new Country();
35+
uk.setName("United Kingdom");
36+
uk.setCapital("London");
37+
uk.setCurrency(Currency.GBP);
38+
uk.setPopulation(63705000);
39+
40+
countries.put(uk.getName(), uk);
41+
}
42+
43+
public Country findCountry(String name) {
44+
Assert.notNull(name, "The country's name must not be null");
45+
return countries.get(name);
46+
}
47+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.baeldung.springsoap;
2+
3+
import org.springframework.boot.web.servlet.ServletRegistrationBean;
4+
import org.springframework.context.ApplicationContext;
5+
import org.springframework.context.annotation.Bean;
6+
import org.springframework.context.annotation.Configuration;
7+
import org.springframework.core.io.ClassPathResource;
8+
import org.springframework.ws.config.annotation.EnableWs;
9+
import org.springframework.ws.config.annotation.WsConfigurerAdapter;
10+
import org.springframework.ws.transport.http.MessageDispatcherServlet;
11+
import org.springframework.ws.wsdl.wsdl11.DefaultWsdl11Definition;
12+
import org.springframework.xml.xsd.SimpleXsdSchema;
13+
import org.springframework.xml.xsd.XsdSchema;
14+
15+
@EnableWs
16+
@Configuration
17+
public class WebServiceConfig extends WsConfigurerAdapter {
18+
19+
@Bean
20+
public ServletRegistrationBean messageDispatcherServlet(ApplicationContext applicationContext) {
21+
MessageDispatcherServlet servlet = new MessageDispatcherServlet();
22+
servlet.setApplicationContext(applicationContext);
23+
servlet.setTransformWsdlLocations(true);
24+
return new ServletRegistrationBean(servlet, "/ws/*");
25+
}
26+
27+
@Bean(name = "countries")
28+
public DefaultWsdl11Definition defaultWsdl11Definition(XsdSchema countriesSchema) {
29+
DefaultWsdl11Definition wsdl11Definition = new DefaultWsdl11Definition();
30+
wsdl11Definition.setPortTypeName("CountriesPort");
31+
wsdl11Definition.setLocationUri("/ws");
32+
wsdl11Definition.setTargetNamespace("http://www.baeldung.com/springsoap/gen");
33+
wsdl11Definition.setSchema(countriesSchema);
34+
return wsdl11Definition;
35+
}
36+
37+
@Bean
38+
public XsdSchema countriesSchema() {
39+
return new SimpleXsdSchema(new ClassPathResource("countries.xsd"));
40+
}
41+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://www.baeldung.com/springsoap/gen"
3+
targetNamespace="http://www.baeldung.com/springsoap/gen" elementFormDefault="qualified">
4+
5+
<xs:element name="getCountryRequest">
6+
<xs:complexType>
7+
<xs:sequence>
8+
<xs:element name="name" type="xs:string"/>
9+
</xs:sequence>
10+
</xs:complexType>
11+
</xs:element>
12+
13+
<xs:element name="getCountryResponse">
14+
<xs:complexType>
15+
<xs:sequence>
16+
<xs:element name="country" type="tns:country"/>
17+
</xs:sequence>
18+
</xs:complexType>
19+
</xs:element>
20+
21+
<xs:complexType name="country">
22+
<xs:sequence>
23+
<xs:element name="name" type="xs:string"/>
24+
<xs:element name="population" type="xs:int"/>
25+
<xs:element name="capital" type="xs:string"/>
26+
<xs:element name="currency" type="tns:currency"/>
27+
</xs:sequence>
28+
</xs:complexType>
29+
30+
<xs:simpleType name="currency">
31+
<xs:restriction base="xs:string">
32+
<xs:enumeration value="GBP"/>
33+
<xs:enumeration value="EUR"/>
34+
<xs:enumeration value="PLN"/>
35+
</xs:restriction>
36+
</xs:simpleType>
37+
</xs:schema>
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.baeldung.springsoap;
2+
3+
import com.baeldung.springsoap.gen.GetCountryRequest;
4+
import org.junit.Before;
5+
import org.junit.Test;
6+
import org.junit.runner.RunWith;
7+
import org.springframework.boot.test.context.SpringBootTest;
8+
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
9+
import org.springframework.boot.web.server.LocalServerPort;
10+
import org.springframework.oxm.jaxb.Jaxb2Marshaller;
11+
import org.springframework.test.context.junit4.SpringRunner;
12+
import org.springframework.util.ClassUtils;
13+
import org.springframework.ws.client.core.WebServiceTemplate;
14+
15+
import static org.assertj.core.api.Assertions.assertThat;
16+
17+
@RunWith(SpringRunner.class)
18+
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
19+
public class ApplicationIntegrationTest {
20+
21+
private Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
22+
23+
@LocalServerPort private int port = 0;
24+
25+
@Before
26+
public void init() throws Exception {
27+
marshaller.setPackagesToScan(ClassUtils.getPackageName(GetCountryRequest.class));
28+
marshaller.afterPropertiesSet();
29+
}
30+
31+
@Test
32+
public void whenSendRequest_thenResponseIsNotNull() {
33+
WebServiceTemplate ws = new WebServiceTemplate(marshaller);
34+
GetCountryRequest request = new GetCountryRequest();
35+
request.setName("Spain");
36+
37+
assertThat(ws.marshalSendAndReceive("http://localhost:" + port + "/ws", request)).isNotNull();
38+
}
39+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
2+
xmlns:gs="http://www.baeldung.com/springsoap/gen">
3+
<soapenv:Header/>
4+
<soapenv:Body>
5+
<gs:getCountryRequest>
6+
<gs:name>Spain</gs:name>
7+
</gs:getCountryRequest>
8+
</soapenv:Body>
9+
</soapenv:Envelope>
10+
11+
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
12+
<SOAP-ENV:Header/>
13+
<SOAP-ENV:Body>
14+
<ns2:getCountryResponse xmlns:ns2="http://www.baeldung.com/springsoap/gen">
15+
<ns2:country>
16+
<ns2:name>Spain</ns2:name>
17+
<ns2:population>46704314</ns2:population>
18+
<ns2:capital>Madrid</ns2:capital>
19+
<ns2:currency>EUR</ns2:currency>
20+
</ns2:country>
21+
</ns2:getCountryResponse>
22+
</SOAP-ENV:Body>
23+
</SOAP-ENV:Envelope>

0 commit comments

Comments
 (0)