From 452d3fdddb3e65a2b99e2a653119c96b13182ae6 Mon Sep 17 00:00:00 2001 From: Michael Pratt Date: Thu, 16 Feb 2023 01:41:00 +0000 Subject: [PATCH 1/2] BAEL-6162: Code examples from article (#13458) * Code example from article * BAEL-6162: Add unit tests * Rename test class --- .../spring-resttemplate-3/README.md | 1 + .../spring-resttemplate-3/pom.xml | 2 +- .../RestTemplateMethodsApplication.java | 102 ++++++++++++++++++ .../methods/RestTemplateMethodsUnitTest.java | 86 +++++++++++++++ 4 files changed, 190 insertions(+), 1 deletion(-) create mode 100644 spring-web-modules/spring-resttemplate-3/src/main/java/com/baeldung/resttemplate/methods/RestTemplateMethodsApplication.java create mode 100644 spring-web-modules/spring-resttemplate-3/src/test/java/com/baeldung/resttemplate/methods/RestTemplateMethodsUnitTest.java diff --git a/spring-web-modules/spring-resttemplate-3/README.md b/spring-web-modules/spring-resttemplate-3/README.md index 52eba522a5..3add0073b2 100644 --- a/spring-web-modules/spring-resttemplate-3/README.md +++ b/spring-web-modules/spring-resttemplate-3/README.md @@ -11,3 +11,4 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring - [Download a Large File Through a Spring RestTemplate](https://www.baeldung.com/spring-resttemplate-download-large-file) - [Access HTTPS REST Service Using Spring RestTemplate](https://www.baeldung.com/spring-resttemplate-secure-https-service) - [Encoding of URI Variables on RestTemplate](https://www.baeldung.com/spring-resttemplate-uri-variables-encode) +- [Difference Between exchange(), postForEntity() and execute() in RestTemplate](https://www.baeldung.com/difference-between-exchange-postForEntity-and-execute) diff --git a/spring-web-modules/spring-resttemplate-3/pom.xml b/spring-web-modules/spring-resttemplate-3/pom.xml index b036a5ffcb..5ce7d348a2 100644 --- a/spring-web-modules/spring-resttemplate-3/pom.xml +++ b/spring-web-modules/spring-resttemplate-3/pom.xml @@ -26,4 +26,4 @@ - \ No newline at end of file + diff --git a/spring-web-modules/spring-resttemplate-3/src/main/java/com/baeldung/resttemplate/methods/RestTemplateMethodsApplication.java b/spring-web-modules/spring-resttemplate-3/src/main/java/com/baeldung/resttemplate/methods/RestTemplateMethodsApplication.java new file mode 100644 index 0000000000..72201aa5c9 --- /dev/null +++ b/spring-web-modules/spring-resttemplate-3/src/main/java/com/baeldung/resttemplate/methods/RestTemplateMethodsApplication.java @@ -0,0 +1,102 @@ +package com.baeldung.resttemplate.methods; + +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; +import org.springframework.http.ResponseEntity; +import org.springframework.http.client.ClientHttpRequest; +import org.springframework.http.client.ClientHttpResponse; +import org.springframework.web.client.RequestCallback; +import org.springframework.web.client.ResponseExtractor; +import org.springframework.web.client.RestTemplate; + +import java.io.IOException; + +/** + * Examples of making the same call with RestTemplate + * using postForEntity(), exchange(), and execute(). + */ +@SpringBootApplication +public class RestTemplateMethodsApplication { + private final static RestTemplate restTemplate = new RestTemplate(); + + public static void main(String[] args) { + + } + + private static void postForEntity() { + Book book = new Book( + "Cruising Along with Java", + "Venkat Subramaniam", + 2023); + + ResponseEntity response = restTemplate.postForEntity( + "https://api.bookstore.com", + book, + Book.class); + } + + private static void exchange() { + Book book = new Book( + "Effective Java", + "Joshua Bloch", + 2001); + + HttpHeaders headers = new HttpHeaders(); + headers.setBasicAuth("username", "password"); + + ResponseEntity response = restTemplate.exchange( + "https://api.bookstore.com", + HttpMethod.POST, + new HttpEntity<>(book, headers), + Book.class); + } + + private static void execute() { + ResponseEntity response = restTemplate.execute( + "https://api.bookstore.com", + HttpMethod.POST, + new RequestCallback() { + @Override + public void doWithRequest(ClientHttpRequest request) throws IOException { + // Create or decorate the request object as needed + } + }, + new ResponseExtractor>() { + @Override + public ResponseEntity extractData(ClientHttpResponse response) throws IOException { + // extract required data from response + return null; + } + } + ); + + // Could also use some factory methods in RestTemplate for + // the request callback and/or response extractor + + Book book = new Book( + "Reactive Spring", + "Josh Long", + 2020); + + response = restTemplate.execute( + "https://api.bookstore.com", + HttpMethod.POST, + restTemplate.httpEntityCallback(book), + restTemplate.responseEntityExtractor(Book.class) + ); + } + + private static class Book { + String title; + String author; + int yearPublished; + + public Book(String title, String author, int yearPublished) { + this.title = title; + this.author = author; + this.yearPublished = yearPublished; + } + } +} diff --git a/spring-web-modules/spring-resttemplate-3/src/test/java/com/baeldung/resttemplate/methods/RestTemplateMethodsUnitTest.java b/spring-web-modules/spring-resttemplate-3/src/test/java/com/baeldung/resttemplate/methods/RestTemplateMethodsUnitTest.java new file mode 100644 index 0000000000..c7a2880f9d --- /dev/null +++ b/spring-web-modules/spring-resttemplate-3/src/test/java/com/baeldung/resttemplate/methods/RestTemplateMethodsUnitTest.java @@ -0,0 +1,86 @@ +package com.baeldung.resttemplate.methods; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.http.*; +import org.springframework.test.web.client.MockRestServiceServer; +import org.springframework.test.web.client.response.MockRestResponseCreators; +import org.springframework.web.client.RestTemplate; + +import static org.springframework.test.web.client.match.MockRestRequestMatchers.method; +import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo; + +/** + * Unit tests for different ways to send POST with RestTemplate. + */ +public class RestTemplateMethodsUnitTest +{ + + private final RestTemplate restTemplate = new RestTemplate(); + + private final String URL = "https://localhost:8080"; + + private MockRestServiceServer mockServer; + + @BeforeEach + public void setup() { + mockServer = MockRestServiceServer.createServer(restTemplate); + } + + /** + * Test that postForEntity sends a POST to the desired URL. + */ + @Test + public void testPostForEntity() { + + mockServer.expect(requestTo(URL)) + .andExpect(method(HttpMethod.POST)) + .andRespond(MockRestResponseCreators.withStatus(HttpStatus.OK) + .contentType(MediaType.TEXT_PLAIN) + .body("Ok")); + + restTemplate.postForEntity( + URL, + "Test Body", + String.class); + + mockServer.verify(); + } + + /** + * Test that exchange with POST method sends a POST to the desired URL. + */ + @Test + public void testPostExchange() { + mockServer.expect(requestTo(URL)) + .andExpect(method(HttpMethod.POST)) + .andRespond(MockRestResponseCreators.withStatus(HttpStatus.OK) + .contentType(MediaType.TEXT_PLAIN) + .body("Ok")); + + restTemplate.exchange( + URL, + HttpMethod.POST, + new HttpEntity<>("Test Body"), + String.class); + + mockServer.verify(); + } + + @Test + public void testPostExecute() { + mockServer.expect(requestTo(URL)) + .andExpect(method(HttpMethod.POST)) + .andRespond(MockRestResponseCreators.withStatus(HttpStatus.OK) + .contentType(MediaType.TEXT_PLAIN) + .body("Ok")); + + restTemplate.execute( + URL, + HttpMethod.POST, + restTemplate.httpEntityCallback("Test body"), + restTemplate.responseEntityExtractor(String.class)); + + mockServer.verify(); + } +} From d604623892faebb46cfe4e2d54a28508cee9f589 Mon Sep 17 00:00:00 2001 From: panos-kakos <102670093+panos-kakos@users.noreply.github.com> Date: Thu, 16 Feb 2023 17:57:31 +0200 Subject: [PATCH 2/2] [JAVA-16376] Moved spring-reactive-modules to jdk9-and-above profile (#13470) * [JAVA-16376] Moved spring-reactive-modules to jdk9-and-above profile * [JAVA-16376] Upgraded geotools dependencies and moved module to jdk9-and-above profile * [JAVA-16376] Replaced vividsolutions artifact with locationtech * [JAVA-16376] Moved data-structures to jdk9-and-above profile * [JAVA-16376] Moved deeplearning4j module to jdk9-and-above profile --------- Co-authored-by: Dhawal Kapil --- deeplearning4j/pom.xml | 9 +++++++ geotools/pom.xml | 13 ++++++--- .../java/com/baeldung/geotools/ShapeFile.java | 8 +++--- pom.xml | 27 +++++++------------ 4 files changed, 32 insertions(+), 25 deletions(-) diff --git a/deeplearning4j/pom.xml b/deeplearning4j/pom.xml index c63e67d573..01bac93214 100644 --- a/deeplearning4j/pom.xml +++ b/deeplearning4j/pom.xml @@ -57,11 +57,20 @@ httpclient ${httpclient.version} + + + org.projectlombok + lombok + ${lombok.version} + provided + + 0.9.1 4.3.5 + 1.18.20 \ No newline at end of file diff --git a/geotools/pom.xml b/geotools/pom.xml index 05cae922a4..6edb344c8c 100644 --- a/geotools/pom.xml +++ b/geotools/pom.xml @@ -39,12 +39,19 @@ gt-swing ${geotools-swing.version} + + org.locationtech.jts + jts-core + 1.19.0 + + + - 15.2 - 15.2 - 15.2 + 28.1 + 28.1 + 28.1 \ No newline at end of file diff --git a/geotools/src/main/java/com/baeldung/geotools/ShapeFile.java b/geotools/src/main/java/com/baeldung/geotools/ShapeFile.java index de789918cd..64650fb486 100644 --- a/geotools/src/main/java/com/baeldung/geotools/ShapeFile.java +++ b/geotools/src/main/java/com/baeldung/geotools/ShapeFile.java @@ -1,8 +1,8 @@ package com.baeldung.geotools; -import com.vividsolutions.jts.geom.Coordinate; -import com.vividsolutions.jts.geom.GeometryFactory; -import com.vividsolutions.jts.geom.Point; +import org.locationtech.jts.geom.Coordinate; +import org.locationtech.jts.geom.GeometryFactory; +import org.locationtech.jts.geom.Point; import org.geotools.data.DataUtilities; import org.geotools.data.DefaultTransaction; import org.geotools.data.Transaction; @@ -35,7 +35,7 @@ public class ShapeFile { DefaultFeatureCollection collection = new DefaultFeatureCollection(); - GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory(null); + GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory(null); SimpleFeatureType TYPE = DataUtilities.createType("Location", "location:Point:srid=4326," + "name:String"); diff --git a/pom.xml b/pom.xml index 0b63defb49..8b395d44f1 100644 --- a/pom.xml +++ b/pom.xml @@ -341,16 +341,9 @@ core-java-modules couchbase custom-pmd - data-structures - deeplearning4j + drools - - - - - geotools - gradle-modules/gradle/maven-to-gradle @@ -470,7 +463,6 @@ server-modules spf4j spring-4 - spring-reactive-modules spring-aop spring-aop-2 spring-batch @@ -618,16 +610,8 @@ core-java-modules couchbase custom-pmd - data-structures - deeplearning4j drools - - - - - geotools - gradle-modules/gradle/maven-to-gradle @@ -739,7 +723,6 @@ server-modules spf4j spring-4 - spring-reactive-modules spring-aop spring-aop-2 spring-batch @@ -940,7 +923,9 @@ core-java-modules/core-java-strings core-java-modules/core-java-httpclient spring-core-6 + data-structures ddd-contexts + deeplearning4j docker-modules apache-httpclient-2 kubernetes-modules/kubernetes-spring @@ -959,11 +944,13 @@ spring-boot-modules/spring-boot-3-native spring-boot-modules/spring-boot-3-observation spring-boot-modules/spring-boot-3-test-pitfalls + spring-reactive-modules spring-swagger-codegen/custom-validations-opeanpi-codegen testing-modules/testing-assertions persistence-modules/fauna lightrun tablesaw + geotools @@ -1145,7 +1132,9 @@ core-java-modules/core-java-strings core-java-modules/core-java-httpclient spring-core-6 + data-structures ddd-contexts + deeplearning4j docker-modules apache-httpclient-2 kubernetes-modules/kubernetes-spring @@ -1164,11 +1153,13 @@ spring-boot-modules/spring-boot-3-native spring-boot-modules/spring-boot-3-observation spring-boot-modules/spring-boot-3-test-pitfalls + spring-reactive-modules spring-swagger-codegen/custom-validations-opeanpi-codegen testing-modules/testing-assertions persistence-modules/fauna lightrun tablesaw + geotools