Merge branch 'eugenp:master' into master

This commit is contained in:
Ulisses Lima 2022-05-31 09:38:13 -03:00 committed by GitHub
commit c3059250e9
972 changed files with 1121 additions and 311 deletions

View File

@ -1,46 +0,0 @@
package com.baeldung.urlconnection;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class PostJSONWithHttpURLConnection {
public static void main (String []args) throws IOException{
//Change the URL with any other publicly accessible POST resource, which accepts JSON request body
URL url = new URL ("https://reqres.in/api/users");
HttpURLConnection con = (HttpURLConnection)url.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type", "application/json; utf-8");
con.setRequestProperty("Accept", "application/json");
con.setDoOutput(true);
//JSON String need to be constructed for the specific resource.
//We may construct complex JSON using any third-party JSON libraries such as jackson or org.json
String jsonInputString = "{\"name\": \"Upendra\", \"job\": \"Programmer\"}";
try(OutputStream os = con.getOutputStream()){
byte[] input = jsonInputString.getBytes("utf-8");
os.write(input, 0, input.length);
}
int code = con.getResponseCode();
System.out.println(code);
try(BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(), "utf-8"))){
StringBuilder response = new StringBuilder();
String responseLine = null;
while ((responseLine = br.readLine()) != null) {
response.append(responseLine.trim());
}
System.out.println(response.toString());
}
}
}

View File

@ -0,0 +1,52 @@
package com.baeldung.urlconnection;
import org.junit.Test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import static org.assertj.core.api.Assertions.assertThat;
public class PostJSONWithHttpURLConnectionManualTest {
@Test
public void givenValidURLAndPayload_whenPost_ThenSuccess() throws IOException {
//Change the URL with any other publicly accessible POST resource, which accepts JSON request body
URL url = new URL("https://reqres.in/api/users");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", "AnyAgent");
con.setRequestProperty("Content-Type", "application/json");
con.setRequestProperty("Accept", "application/json");
con.setDoOutput(true);
//JSON String need to be constructed for the specific resource.
//We may construct complex JSON using any third-party JSON libraries such as jackson or org.json
String jsonInputString = "{\"name\": \"Upendra\", \"job\": \"Programmer\"}";
try (OutputStream os = con.getOutputStream()) {
byte[] input = jsonInputString.getBytes(StandardCharsets.UTF_8);
os.write(input, 0, input.length);
}
assertThat(con.getResponseCode()).isEqualTo(201);
try (BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(), StandardCharsets.UTF_8))) {
StringBuilder response = new StringBuilder();
String responseLine = null;
while ((responseLine = br.readLine()) != null) {
response.append(responseLine.trim());
}
assertThat(response).contains("createdAt");
}
}
}

37
javax-sound/pom.xml Normal file
View File

@ -0,0 +1,37 @@
<?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>
<groupId>com.baeldung.javax-sound</groupId>
<artifactId>javax-sound</artifactId>
<version>1.0-SNAPSHOT</version>
<name>javax-sound</name>
<packaging>jar</packaging>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>javazoom</groupId>
<artifactId>jlayer</artifactId>
<version>${javazoom.jlayer.version}</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>${org.openjfx.version}</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-media</artifactId>
<version>${org.openjfx.version}</version>
</dependency>
</dependencies>
<properties>
<org.openjfx.version>11.0.2</org.openjfx.version>
<javazoom.jlayer.version>1.0.1</javazoom.jlayer.version>
</properties>
</project>

View File

@ -0,0 +1,77 @@
package com.baeldung;
import java.io.IOException;
import java.io.InputStream;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineEvent;
import javax.sound.sampled.LineListener;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
public class SoundPlayerUsingClip implements LineListener {
boolean isPlaybackCompleted;
@Override
public void update(LineEvent event) {
if (LineEvent.Type.START == event.getType()) {
System.out.println("Playback started.");
} else if (LineEvent.Type.STOP == event.getType()) {
isPlaybackCompleted = true;
System.out.println("Playback completed.");
}
}
/**
*
* Play a given audio file.
* @param audioFilePath Path of the audio file.
*
*/
void play(String audioFilePath) {
try {
InputStream inputStream = getClass().getClassLoader()
.getResourceAsStream(audioFilePath);
AudioInputStream audioStream = AudioSystem.getAudioInputStream(inputStream);
AudioFormat format = audioStream.getFormat();
DataLine.Info info = new DataLine.Info(Clip.class, format);
Clip audioClip = (Clip) AudioSystem.getLine(info);
audioClip.addLineListener(this);
audioClip.open(audioStream);
audioClip.start();
while (!isPlaybackCompleted) {
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
audioClip.close();
audioStream.close();
} catch (UnsupportedAudioFileException | LineUnavailableException | IOException ex) {
System.out.println("Error occured during playback process:"+ ex.getMessage());
}
}
public static void main(String[] args) {
String audioFilePath = "AudioFileWithWavFormat.wav";
// Clip can not play mpeg/mp3 format audio. We'll get exception if we run with below commented mp3 and mpeg format audio.
// String audioFilePath = "AudioFileWithMpegFormat.mpeg";
// String audioFilePath = "AudioFileWithMp3Format.mp3";
SoundPlayerUsingClip player = new SoundPlayerUsingClip();
player.play(audioFilePath);
}
}

View File

@ -0,0 +1,39 @@
package com.baeldung;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
public class SoundPlayerUsingJavaFx {
public static void main(String[] args) {
// String audioFilePath = "AudioFileWithWavFormat.wav";
// String audioFilePath = "AudioFileWithMpegFormat.mpeg";
String audioFilePath = "AudioFileWithMp3Format.mp3";
SoundPlayerUsingJavaFx soundPlayerWithJavaFx = new SoundPlayerUsingJavaFx();
try {
com.sun.javafx.application.PlatformImpl.startup(() -> {
});
Media media = new Media(soundPlayerWithJavaFx.getClass()
.getClassLoader()
.getResource(audioFilePath)
.toExternalForm());
MediaPlayer mp3Player = new MediaPlayer(media);
mp3Player.setOnPlaying(new Runnable() {
@Override
public void run() {
System.out.println("Playback started");
}
});
mp3Player.play();
} catch (Exception ex) {
System.out.println("Error occured during playback process:" + ex.getMessage());
}
}
}

View File

@ -0,0 +1,30 @@
package com.baeldung;
import java.io.BufferedInputStream;
import javazoom.jl.player.Player;
public class SoundPlayerUsingJavaZoom {
public static void main(String[] args) {
// javazoom Player doesn't work with wav audio format.
// String audioFilePath = "AudioFileWithWavFormat.wav";
// It works with below audio formats.
// String audioFilePath = "AudioFileWithMpegFormat.mpeg";
String audioFilePath = "AudioFileWithMp3Format.mp3";
SoundPlayerUsingJavaZoom player = new SoundPlayerUsingJavaZoom();
try {
BufferedInputStream buffer = new BufferedInputStream(player.getClass()
.getClassLoader()
.getResourceAsStream(audioFilePath));
Player mp3Player = new Player(buffer);
mp3Player.play();
} catch (Exception ex) {
System.out.println("Error occured during playback process:" + ex.getMessage());
}
}
}

View File

@ -0,0 +1,59 @@
package com.baeldung;
import java.io.IOException;
import java.io.InputStream;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.UnsupportedAudioFileException;
public class SoundPlayerUsingSourceDataLine {
private static final int BUFFER_SIZE = 4096;
void play(String soundFilePath) {
try {
InputStream inputStream = getClass().getClassLoader()
.getResourceAsStream(soundFilePath);
AudioInputStream audioStream = AudioSystem.getAudioInputStream(inputStream);
AudioFormat audioFormat = audioStream.getFormat();
DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);
SourceDataLine sourceDataLine = (SourceDataLine) AudioSystem.getLine(info);
sourceDataLine.open(audioFormat);
sourceDataLine.start();
System.out.println("Playback Started.");
byte[] bufferBytes = new byte[BUFFER_SIZE];
int readBytes = -1;
while ((readBytes = audioStream.read(bufferBytes)) != -1) {
sourceDataLine.write(bufferBytes, 0, readBytes);
}
sourceDataLine.drain();
sourceDataLine.close();
audioStream.close();
System.out.println("Playback has been finished.");
} catch (UnsupportedAudioFileException | LineUnavailableException | IOException ex) {
System.out.println("Error occured during playback process:" + ex.getMessage());
}
}
public static void main(String[] args) {
String audioFilePath = "AudioFileWithWavFormat.wav";
// Clip can not play mpeg/mp3 format audio. We'll get exception if we run with below commented mp3 and mpeg format audio.
// String audioFilePath = "AudioFileWithMpegFormat.mpeg";
// String audioFilePath = "AudioFileWithMp3Format.mp3";
SoundPlayerUsingSourceDataLine player = new SoundPlayerUsingSourceDataLine();
player.play(audioFilePath);
}
}

View File

@ -20,14 +20,11 @@
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>java-hamcrest</artifactId>
<version>${org.hamcrest.java-hamcrest.version}</version>
<artifactId>hamcrest</artifactId>
<version>${hamcrest.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<properties>
<org.hamcrest.java-hamcrest.version>2.0.0.0</org.hamcrest.java-hamcrest.version>
</properties>
</project>

View File

@ -107,7 +107,6 @@
<docx4j.version>3.3.5</docx4j.version>
<jaxb-api.version>2.1</jaxb-api.version>
<gson.version>2.8.7</gson.version>
<jackson.version>2.12.3</jackson.version>
<yamlbeans.version>1.15</yamlbeans.version>
<apache-thrift.version>0.14.2</apache-thrift.version>
<google-protobuf.version>3.17.3</google-protobuf.version>

View File

@ -69,12 +69,6 @@
<properties>
<fastutil.version>8.2.2</fastutil.version>
<eclipse-collections.version>10.0.0</eclipse-collections.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<jmh-core.version>1.28</jmh-core.version>
<jmh-generator.version>1.28</jmh-generator.version>
<junit-jupiter.version>5.8.1</junit-jupiter.version>
<maven-surefire-plugin.version>2.22.2</maven-surefire-plugin.version>
</properties>
</project>

View File

@ -171,15 +171,6 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>${maven-compiler-plugin.source}</source>
<target>${maven-compiler-plugin.target}</target>
</configuration>
</plugin>
</plugins>
</build>
@ -196,10 +187,6 @@
<spring-mock-mvc.version>4.1.1</spring-mock-mvc.version>
<java-hamcrest.version>2.0.0.0</java-hamcrest.version>
<dbunit.version>2.7.0</dbunit.version>
<assertj-core.version>3.14.0</assertj-core.version>
<maven-compiler-plugin.target>1.8</maven-compiler-plugin.target>
<maven-compiler-plugin.source>1.8</maven-compiler-plugin.source>
<maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version>
<archunit.version>0.14.1</archunit.version>
<modelassert.version>1.0.0</modelassert.version>
</properties>

View File

@ -78,7 +78,7 @@
<dependency>
<groupId>com.netflix.spectator</groupId>
<artifactId>spectator-api</artifactId>
<version>1.0.11</version>
<version>${spectator-api.version}</version>
</dependency>
</dependencies>
@ -90,6 +90,7 @@
<!-- <fasterxml.jackson.version>2.9.1</fasterxml.jackson.version> -->
<spring-boot-starter-web.version>2.0.7.RELEASE</spring-boot-starter-web.version>
<metrics-aspectj.version>1.1.0</metrics-aspectj.version>
<spectator-api.version>1.0.11</spectator-api.version>
</properties>
</project>

View File

@ -23,7 +23,7 @@
<dependency>
<groupId>io.mantisrx</groupId>
<artifactId>mantis-runtime</artifactId>
<version>1.2.63</version>
<version>${mantis-runtime.version}</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
@ -38,7 +38,7 @@
<dependency>
<groupId>net.andreinc.mockneat</groupId>
<artifactId>mockneat</artifactId>
<version>0.4.2</version>
<version>${mockneat.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
@ -63,4 +63,9 @@
</repository>
</repositories>
<properties>
<mantis-runtime.version>1.2.63</mantis-runtime.version>
<mockneat.version>0.4.2</mockneat.version>
</properties>
</project>

View File

@ -25,10 +25,4 @@
</dependency>
</dependencies>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<lombok.version>1.18.12</lombok.version>
</properties>
</project>

View File

@ -437,6 +437,7 @@
<module>java-websocket</module>
<module>javax-servlets</module>
<module>javax-servlets-2</module>
<module>javax-sound</module>
<module>javaxval</module>
<module>jaxb</module>
<module>jee-7</module>
@ -615,7 +616,7 @@
<module>spring-caching</module>
<module>spring-caching-2</module>
<module>spring-cloud</module>
<module>spring-cloud-modules</module>
<module>spring-cloud-bus</module>
<!-- <module>spring-cloud-cli</module> --> <!-- Not a maven project -->
<module>spring-cloud-data-flow</module>
@ -898,6 +899,7 @@
<module>java-websocket</module>
<module>javax-servlets</module>
<module>javax-servlets-2</module>
<module>javax-sound</module>
<module>javaxval</module>
<module>jaxb</module>
<module>jee-7</module>
@ -1070,7 +1072,7 @@
<module>spring-caching</module>
<module>spring-caching-2</module>
<module>spring-cloud</module>
<module>spring-cloud-modules</module>
<module>spring-cloud-bus</module>
<!-- <module>spring-cloud-cli</module> --> <!-- Not a maven project -->
<module>spring-cloud-data-flow</module>

View File

@ -19,13 +19,52 @@
<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>
<dependency>
<groupId>wsdl4j</groupId>
<artifactId>wsdl4j</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.ws</groupId>
<artifactId>spring-ws-test</artifactId>
<version>3.1.3</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<version>0.15.1</version>
<executions>
<execution>
<id>xjc</id>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<schemaDirectory>${project.basedir}/src/main/resources/webservice</schemaDirectory>
<generateDirectory>${project.basedir}/src/main/java</generateDirectory>
<generatePackage>com.baeldung.webservice.generated</generatePackage>
<episode>false</episode>
<noFileHeader>true</noFileHeader>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<properties>
<start-class>com.baeldung.boot.Application</start-class>
</properties>

View File

@ -0,0 +1,15 @@
package com.baeldung.webservice;
import com.baeldung.webservice.generated.Product;
import org.springframework.stereotype.Component;
@Component
public class InMemoryProductRepository implements ProductRepository {
public Product findProduct(String id) {
Product product = new Product();
product.setId(id);
product.setName("Product " + id);
return product;
}
}

View File

@ -0,0 +1,27 @@
package com.baeldung.webservice;
import com.baeldung.webservice.generated.GetProductRequest;
import com.baeldung.webservice.generated.GetProductResponse;
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;
@Endpoint
public class ProductEndpoint {
private static final String NAMESPACE_URI = "http://baeldung.com/spring-boot-web-service";
@Autowired
private ProductRepository productRepository;
@PayloadRoot(namespace = NAMESPACE_URI, localPart = "getProductRequest")
@ResponsePayload
public GetProductResponse getProduct(@RequestPayload GetProductRequest request) {
GetProductResponse response = new GetProductResponse();
response.setProduct(productRepository.findProduct(request.getId()));
return response;
}
}

View File

@ -0,0 +1,9 @@
package com.baeldung.webservice;
import com.baeldung.webservice.generated.Product;
public interface ProductRepository {
Product findProduct(String id);
}

View File

@ -0,0 +1,13 @@
package com.baeldung.webservice;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class WebServiceApplication {
public static void main(String[] args) {
SpringApplication.run(WebServiceApplication.class, args);
}
}

View File

@ -0,0 +1,44 @@
package com.baeldung.webservice.config;
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.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 {
@Bean
public ServletRegistrationBean<MessageDispatcherServlet> messageDispatcherServlet(ApplicationContext applicationContext) {
MessageDispatcherServlet servlet = new MessageDispatcherServlet();
servlet.setApplicationContext(applicationContext);
servlet.setTransformWsdlLocations(true);
return new ServletRegistrationBean<>(servlet, "/ws/*");
}
@Bean(name = "products")
public DefaultWsdl11Definition defaultWsdl11Definition(XsdSchema productsSchema) {
DefaultWsdl11Definition wsdl11Definition = new DefaultWsdl11Definition();
wsdl11Definition.setPortTypeName("ProductsPort");
wsdl11Definition.setLocationUri("/ws");
wsdl11Definition.setTargetNamespace("http://baeldung.com/spring-boot-web-service");
wsdl11Definition.setSchema(productsSchema);
return wsdl11Definition;
}
@Bean
public XsdSchema productsSchema() {
return new SimpleXsdSchema(new ClassPathResource("webservice/products.xsd"));
}
}

View File

@ -0,0 +1,64 @@
package com.baeldung.webservice.generated;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
/**
* <p>Java class for anonymous complex type.
*
* <p>The following schema fragment specifies the expected content contained within this class.
*
* <pre>
* &lt;complexType&gt;
* &lt;complexContent&gt;
* &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType"&gt;
* &lt;sequence&gt;
* &lt;element name="id" type="{http://www.w3.org/2001/XMLSchema}string"/&gt;
* &lt;/sequence&gt;
* &lt;/restriction&gt;
* &lt;/complexContent&gt;
* &lt;/complexType&gt;
* </pre>
*
*
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"id"
})
@XmlRootElement(name = "getProductRequest")
public class GetProductRequest {
@XmlElement(required = true)
protected String id;
/**
* Gets the value of the id property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getId() {
return id;
}
/**
* Sets the value of the id property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setId(String value) {
this.id = value;
}
}

View File

@ -0,0 +1,64 @@
package com.baeldung.webservice.generated;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
/**
* <p>Java class for anonymous complex type.
*
* <p>The following schema fragment specifies the expected content contained within this class.
*
* <pre>
* &lt;complexType&gt;
* &lt;complexContent&gt;
* &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType"&gt;
* &lt;sequence&gt;
* &lt;element name="product" type="{http://baeldung.com/spring-boot-web-service}product"/&gt;
* &lt;/sequence&gt;
* &lt;/restriction&gt;
* &lt;/complexContent&gt;
* &lt;/complexType&gt;
* </pre>
*
*
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"product"
})
@XmlRootElement(name = "getProductResponse")
public class GetProductResponse {
@XmlElement(required = true)
protected Product product;
/**
* Gets the value of the product property.
*
* @return
* possible object is
* {@link Product }
*
*/
public Product getProduct() {
return product;
}
/**
* Sets the value of the product property.
*
* @param value
* allowed object is
* {@link Product }
*
*/
public void setProduct(Product value) {
this.product = value;
}
}

View File

@ -0,0 +1,56 @@
package com.baeldung.webservice.generated;
import javax.xml.bind.annotation.XmlRegistry;
/**
* This object contains factory methods for each
* Java content interface and Java element interface
* generated in the com.baeldung.webservice.generated package.
* <p>An ObjectFactory allows you to programatically
* construct new instances of the Java representation
* for XML content. The Java representation of XML
* content can consist of schema derived interfaces
* and classes representing the binding of schema
* type definitions, element declarations and model
* groups. Factory methods for each of these are
* provided in this class.
*
*/
@XmlRegistry
public class ObjectFactory {
/**
* Create a new ObjectFactory that can be used to create new instances of schema derived classes for package: com.baeldung.webservice.generated
*
*/
public ObjectFactory() {
}
/**
* Create an instance of {@link GetProductRequest }
*
*/
public GetProductRequest createGetProductRequest() {
return new GetProductRequest();
}
/**
* Create an instance of {@link GetProductResponse }
*
*/
public GetProductResponse createGetProductResponse() {
return new GetProductResponse();
}
/**
* Create an instance of {@link Product }
*
*/
public Product createProduct() {
return new Product();
}
}

View File

@ -0,0 +1,90 @@
package com.baeldung.webservice.generated;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;
/**
* <p>Java class for product complex type.
*
* <p>The following schema fragment specifies the expected content contained within this class.
*
* <pre>
* &lt;complexType name="product"&gt;
* &lt;complexContent&gt;
* &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType"&gt;
* &lt;sequence&gt;
* &lt;element name="id" type="{http://www.w3.org/2001/XMLSchema}string"/&gt;
* &lt;element name="name" type="{http://www.w3.org/2001/XMLSchema}string"/&gt;
* &lt;/sequence&gt;
* &lt;/restriction&gt;
* &lt;/complexContent&gt;
* &lt;/complexType&gt;
* </pre>
*
*
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "product", propOrder = {
"id",
"name"
})
public class Product {
@XmlElement(required = true)
protected String id;
@XmlElement(required = true)
protected String name;
/**
* Gets the value of the id property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getId() {
return id;
}
/**
* Sets the value of the id property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setId(String value) {
this.id = value;
}
/**
* Gets the value of the name property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getName() {
return name;
}
/**
* Sets the value of the name property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setName(String value) {
this.name = value;
}
}

View File

@ -0,0 +1,2 @@
@javax.xml.bind.annotation.XmlSchema(namespace = "http://baeldung.com/spring-boot-web-service", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
package com.baeldung.webservice.generated;

View File

@ -0,0 +1,26 @@
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:bd="http://baeldung.com/spring-boot-web-service"
targetNamespace="http://baeldung.com/spring-boot-web-service" elementFormDefault="qualified">
<xs:element name="getProductRequest">
<xs:complexType>
<xs:sequence>
<xs:element name="id" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="getProductResponse">
<xs:complexType>
<xs:sequence>
<xs:element name="product" type="bd:product"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="product">
<xs:sequence>
<xs:element name="id" type="xs:string"/>
<xs:element name="name" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:schema>

View File

@ -0,0 +1,69 @@
package com.baeldung.webservice;
import com.baeldung.webservice.generated.Product;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.webservices.server.WebServiceServerTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.core.io.ClassPathResource;
import org.springframework.ws.test.server.MockWebServiceClient;
import org.springframework.xml.transform.StringSource;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import static org.mockito.Mockito.when;
import static org.springframework.ws.test.server.RequestCreators.withPayload;
import static org.springframework.ws.test.server.ResponseMatchers.noFault;
import static org.springframework.ws.test.server.ResponseMatchers.payload;
import static org.springframework.ws.test.server.ResponseMatchers.validPayload;
import static org.springframework.ws.test.server.ResponseMatchers.xpath;
@WebServiceServerTest
class ProductEndpointIntegrationTest {
private static final Map<String, String> NAMESPACE_MAPPING = createMapping();
@Autowired
private MockWebServiceClient client;
@MockBean
private ProductRepository productRepository;
@Test
void givenXmlRequest_whenServiceInvoked_thenValidResponse() throws IOException {
Product product = new Product();
product.setId("1");
product.setName("Product 1");
when(productRepository.findProduct("1")).thenReturn(product);
StringSource request = new StringSource(
"<bd:getProductRequest xmlns:bd='http://baeldung.com/spring-boot-web-service'>" +
"<bd:id>1</bd:id>" +
"</bd:getProductRequest>"
);
StringSource response = new StringSource(
"<bd:getProductResponse xmlns:bd='http://baeldung.com/spring-boot-web-service'>" +
"<bd:product>" +
"<bd:id>1</bd:id>" +
"<bd:name>Product 1</bd:name>" +
"</bd:product>" +
"</bd:getProductResponse>"
);
client.sendRequest(withPayload(request))
.andExpect(noFault())
.andExpect(validPayload(new ClassPathResource("webservice/products.xsd")))
.andExpect(xpath("/bd:getProductResponse/bd:product[1]/bd:name", NAMESPACE_MAPPING)
.evaluatesTo("Product 1"))
.andExpect(payload(response));
}
private static Map<String, String> createMapping() {
Map<String, String> mapping = new HashMap<>();
mapping.put("bd", "http://baeldung.com/spring-boot-web-service");
return mapping;
}
}

View File

@ -4,9 +4,9 @@
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>
<groupId>com.baeldung.spring.cloud</groupId>
<artifactId>spring-cloud</artifactId>
<artifactId>spring-cloud-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
<name>spring-cloud</name>
<name>spring-cloud-modules</name>
<packaging>pom</packaging>
<parent>

View File

@ -11,7 +11,7 @@
<parent>
<groupId>com.baeldung.spring.cloud</groupId>
<artifactId>spring-cloud</artifactId>
<artifactId>spring-cloud-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>

View File

@ -11,7 +11,7 @@
<parent>
<groupId>com.baeldung.spring.cloud</groupId>
<artifactId>spring-cloud</artifactId>
<artifactId>spring-cloud-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>

Some files were not shown because too many files have changed in this diff Show More