Added working SOAP API

This commit is contained in:
Yohan Boujon 2024-12-10 17:33:48 +01:00
parent cc235da44b
commit d4b0c2979c
20 changed files with 317 additions and 60 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
target/

View file

@ -1,5 +1,3 @@
#Generated by Maven
#Tue Dec 10 16:22:56 CET 2024
artifactId=rest-service
groupId=insa.application.helpapp
version=1.0-SNAPSHOT

View file

@ -1 +1 @@
/home/robin/Desktop/helpapp/rest-service/src/main/java/insa/application/helpapp/rest/RestApplication.java
/home/yoboujon/Documents/GEI/service-architecture/helpapp/rest-service/src/main/java/insa/application/helpapp/rest/RestApplication.java

View file

@ -1,40 +1,73 @@
<project xmlns="http://maven.apache.org/POM/4.0.0">
<parent>
<groupId>insa.application.helpapp</groupId>
<artifactId>helpapp</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>soap-service</artifactId>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.3.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>producing-web-service-complete</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>producing-web-service-complete</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<!-- Dépendance pour créer un service SOAP -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
<!-- tag::springws[] -->
<dependency>
<groupId>wsdl4j</groupId>
<artifactId>wsdl4j</artifactId>
</dependency>
<!-- end::springws[] -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<!-- Plugin Maven pour Spring Boot -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>3.1.4</version>
</plugin>
<!-- Plugin pour configurer le compilateur -->
<!-- tag::xsd[] -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<id>xjc</id>
<goals>
<goal>xjc</goal>
</goals>
</execution>
</executions>
<configuration>
<source>21</source>
<target>21</target>
<sources>
<source>${project.basedir}/src/main/resources/countries.xsd</source>
</sources>
</configuration>
</plugin>
<!-- end::xsd[] -->
</plugins>
</build>
</project>
</project>

View file

@ -0,0 +1 @@
io

View file

@ -0,0 +1,31 @@
package com.example.producingwebservice;
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 org.springframework.ws.server.endpoint.annotation.ResponsePayload;
import io.spring.guides.gs_producing_web_service.GetCountryRequest;
import io.spring.guides.gs_producing_web_service.GetCountryResponse;
@Endpoint
public class CountryEndpoint {
private static final String NAMESPACE_URI = "http://spring.io/guides/gs-producing-web-service";
private CountryRepository countryRepository;
@Autowired
public CountryEndpoint(CountryRepository countryRepository) {
this.countryRepository = countryRepository;
}
@PayloadRoot(namespace = NAMESPACE_URI, localPart = "getCountryRequest")
@ResponsePayload
public GetCountryResponse getCountry(@RequestPayload GetCountryRequest request) {
GetCountryResponse response = new GetCountryResponse();
response.setCountry(countryRepository.findCountry(request.getName()));
return response;
}
}

View file

@ -0,0 +1,47 @@
package com.example.producingwebservice;
import jakarta.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;
import io.spring.guides.gs_producing_web_service.Country;
import io.spring.guides.gs_producing_web_service.Currency;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;
@Component
public class CountryRepository {
private static final Map<String, Country> countries = new HashMap<>();
@PostConstruct
public void initData() {
Country spain = new Country();
spain.setName("Spain");
spain.setCapital("Madrid");
spain.setCurrency(Currency.EUR);
spain.setPopulation(46704314);
countries.put(spain.getName(), spain);
Country poland = new Country();
poland.setName("Poland");
poland.setCapital("Warsaw");
poland.setCurrency(Currency.PLN);
poland.setPopulation(38186860);
countries.put(poland.getName(), poland);
Country uk = new Country();
uk.setName("United Kingdom");
uk.setCapital("London");
uk.setCurrency(Currency.GBP);
uk.setPopulation(63705000);
countries.put(uk.getName(), uk);
}
public Country findCountry(String name) {
Assert.notNull(name, "The country's name must not be null");
return countries.get(name);
}
}

View file

@ -0,0 +1,12 @@
package com.example.producingwebservice;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ProducingWebServiceApplication {
public static void main(String[] args) {
SpringApplication.run(ProducingWebServiceApplication.class, args);
}
}

View file

@ -0,0 +1,40 @@
package com.example.producingwebservice;
import org.springframework.boot.web.servlet.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<MessageDispatcherServlet> messageDispatcherServlet(ApplicationContext applicationContext) {
MessageDispatcherServlet servlet = new MessageDispatcherServlet();
servlet.setApplicationContext(applicationContext);
servlet.setTransformWsdlLocations(true);
return new ServletRegistrationBean<>(servlet, "/ws/*");
}
@Bean(name = "countries")
public DefaultWsdl11Definition defaultWsdl11Definition(XsdSchema countriesSchema) {
DefaultWsdl11Definition wsdl11Definition = new DefaultWsdl11Definition();
wsdl11Definition.setPortTypeName("CountriesPort");
wsdl11Definition.setLocationUri("/ws");
wsdl11Definition.setTargetNamespace("http://spring.io/guides/gs-producing-web-service");
wsdl11Definition.setSchema(countriesSchema);
return wsdl11Definition;
}
@Bean
public XsdSchema countriesSchema() {
return new SimpleXsdSchema(new ClassPathResource("countries.xsd"));
}
}

View file

@ -1,12 +0,0 @@
package insa.application.helpapp.soap;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SoapApplication {
public static void main(String[] args) {
SpringApplication.run(SoapApplication.class, args);
}
}

View file

@ -1 +0,0 @@
server.port=8081

View file

@ -0,0 +1,36 @@
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://spring.io/guides/gs-producing-web-service"
targetNamespace="http://spring.io/guides/gs-producing-web-service" elementFormDefault="qualified">
<xs:element name="getCountryRequest">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="getCountryResponse">
<xs:complexType>
<xs:sequence>
<xs:element name="country" type="tns:country"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="country">
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="population" type="xs:int"/>
<xs:element name="capital" type="xs:string"/>
<xs:element name="currency" type="tns:currency"/>
</xs:sequence>
</xs:complexType>
<xs:simpleType name="currency">
<xs:restriction base="xs:string">
<xs:enumeration value="GBP"/>
<xs:enumeration value="EUR"/>
<xs:enumeration value="PLN"/>
</xs:restriction>
</xs:simpleType>
</xs:schema>

View file

@ -0,0 +1,56 @@
/*
* Copyright 2014-2015 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 com.example.producingwebservice;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import io.spring.guides.gs_producing_web_service.GetCountryRequest;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.server.LocalServerPort;
import org.springframework.oxm.jaxb.Jaxb2Marshaller;
import org.springframework.util.ClassUtils;
import org.springframework.ws.client.core.WebServiceTemplate;
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
public class ProducingWebServiceApplicationIntegrationTests {
private Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
@LocalServerPort
private int port = 0;
@BeforeEach
public void init() throws Exception {
marshaller.setPackagesToScan(ClassUtils.getPackageName(GetCountryRequest.class));
marshaller.afterPropertiesSet();
}
@Test
public void testSendAndReceive() {
WebServiceTemplate ws = new WebServiceTemplate(marshaller);
GetCountryRequest request = new GetCountryRequest();
request.setName("Spain");
assertThat(ws.marshalSendAndReceive("http://localhost:"
+ port + "/ws", request) != null);
}
}

View file

@ -1 +0,0 @@
server.port=8081

View file

@ -1,5 +1,3 @@
#Generated by Maven
#Tue Dec 10 16:22:57 CET 2024
artifactId=soap-service
groupId=insa.application.helpapp
version=1.0-SNAPSHOT
artifactId=producing-web-service-complete
groupId=com.example
version=0.0.1-SNAPSHOT

View file

@ -1 +1,10 @@
insa/application/helpapp/soap/SoapApplication.class
com/example/producingwebservice/ProducingWebServiceApplication.class
com/example/producingwebservice/WebServiceConfig.class
com/example/producingwebservice/CountryRepository.class
io/spring/guides/gs_producing_web_service/GetCountryResponse.class
io/spring/guides/gs_producing_web_service/package-info.class
com/example/producingwebservice/CountryEndpoint.class
io/spring/guides/gs_producing_web_service/GetCountryRequest.class
io/spring/guides/gs_producing_web_service/ObjectFactory.class
io/spring/guides/gs_producing_web_service/Currency.class
io/spring/guides/gs_producing_web_service/Country.class

View file

@ -1 +1,10 @@
/home/robin/Desktop/helpapp/soap-service/src/main/java/insa/application/helpapp/soap/SoapApplication.java
/home/yoboujon/Documents/GEI/service-architecture/helpapp/soap-service/src/main/java/com/example/producingwebservice/CountryEndpoint.java
/home/yoboujon/Documents/GEI/service-architecture/helpapp/soap-service/src/main/java/com/example/producingwebservice/CountryRepository.java
/home/yoboujon/Documents/GEI/service-architecture/helpapp/soap-service/src/main/java/com/example/producingwebservice/ProducingWebServiceApplication.java
/home/yoboujon/Documents/GEI/service-architecture/helpapp/soap-service/src/main/java/com/example/producingwebservice/WebServiceConfig.java
/home/yoboujon/Documents/GEI/service-architecture/helpapp/soap-service/target/generated-sources/jaxb/io/spring/guides/gs_producing_web_service/Country.java
/home/yoboujon/Documents/GEI/service-architecture/helpapp/soap-service/target/generated-sources/jaxb/io/spring/guides/gs_producing_web_service/Currency.java
/home/yoboujon/Documents/GEI/service-architecture/helpapp/soap-service/target/generated-sources/jaxb/io/spring/guides/gs_producing_web_service/GetCountryRequest.java
/home/yoboujon/Documents/GEI/service-architecture/helpapp/soap-service/target/generated-sources/jaxb/io/spring/guides/gs_producing_web_service/GetCountryResponse.java
/home/yoboujon/Documents/GEI/service-architecture/helpapp/soap-service/target/generated-sources/jaxb/io/spring/guides/gs_producing_web_service/ObjectFactory.java
/home/yoboujon/Documents/GEI/service-architecture/helpapp/soap-service/target/generated-sources/jaxb/io/spring/guides/gs_producing_web_service/package-info.java