Merge branch 'eugenp:master' into JAVA-18149

This commit is contained in:
timis1 2023-02-16 18:41:59 +02:00 committed by GitHub
commit ce479f65eb
8 changed files with 222 additions and 26 deletions

View File

@ -57,11 +57,20 @@
<artifactId>httpclient</artifactId>
<version>${httpclient.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
<properties>
<dl4j.version>0.9.1</dl4j.version> <!-- Latest non beta version -->
<httpclient.version>4.3.5</httpclient.version>
<lombok.version>1.18.20</lombok.version>
</properties>
</project>

View File

@ -39,12 +39,19 @@
<artifactId>gt-swing</artifactId>
<version>${geotools-swing.version}</version>
</dependency>
<dependency>
<groupId>org.locationtech.jts</groupId>
<artifactId>jts-core</artifactId>
<version>1.19.0</version>
</dependency>
</dependencies>
<properties>
<geotools.version>15.2</geotools.version>
<geotools-swing.version>15.2</geotools-swing.version>
<geotools-shapefile.version>15.2</geotools-shapefile.version>
<geotools.version>28.1</geotools.version>
<geotools-swing.version>28.1</geotools-swing.version>
<geotools-shapefile.version>28.1</geotools-shapefile.version>
</properties>
</project>

View File

@ -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");

27
pom.xml
View File

@ -341,16 +341,9 @@
<module>core-java-modules</module>
<module>couchbase</module>
<module>custom-pmd</module>
<module>data-structures</module>
<module>deeplearning4j</module>
<module>drools</module>
<!-- <module>ethereum</module> --> <!-- JAVA-6001 -->
<module>geotools</module>
<!-- <module>gradle-modules</module> --> <!-- Not a maven project -->
<module>gradle-modules/gradle/maven-to-gradle</module>
<!-- <module>grails</module> --> <!-- Not a maven project -->
@ -469,7 +462,6 @@
<module>server-modules</module>
<module>spf4j</module>
<module>spring-4</module>
<module>spring-reactive-modules</module>
<module>spring-aop</module>
<module>spring-aop-2</module>
<module>spring-batch</module>
@ -617,16 +609,8 @@
<module>core-java-modules</module>
<module>couchbase</module>
<module>custom-pmd</module>
<module>data-structures</module>
<module>deeplearning4j</module>
<module>drools</module>
<!-- <module>ethereum</module> --> <!-- JAVA-6001 -->
<module>geotools</module>
<!-- <module>gradle-modules</module> --> <!-- Not a maven project -->
<module>gradle-modules/gradle/maven-to-gradle</module>
<!-- <module>grails</module> --> <!-- Not a maven project -->
@ -737,7 +721,6 @@
<module>server-modules</module>
<module>spf4j</module>
<module>spring-4</module>
<module>spring-reactive-modules</module>
<module>spring-aop</module>
<module>spring-aop-2</module>
<module>spring-batch</module>
@ -938,7 +921,9 @@
<module>core-java-modules/core-java-strings</module>
<module>core-java-modules/core-java-httpclient</module>
<module>spring-core-6</module>
<module>data-structures</module>
<module>ddd-contexts</module>
<module>deeplearning4j</module>
<module>docker-modules</module>
<module>apache-httpclient-2</module>
<module>kubernetes-modules/kubernetes-spring</module>
@ -957,11 +942,13 @@
<module>spring-boot-modules/spring-boot-3-native</module>
<module>spring-boot-modules/spring-boot-3-observation</module>
<module>spring-boot-modules/spring-boot-3-test-pitfalls</module>
<module>spring-reactive-modules</module>
<module>spring-swagger-codegen/custom-validations-opeanpi-codegen</module>
<module>testing-modules/testing-assertions</module>
<module>persistence-modules/fauna</module>
<module>lightrun</module>
<module>tablesaw</module>
<module>geotools</module>
<!-- Modules from default-first -->
@ -1144,7 +1131,9 @@
<module>core-java-modules/core-java-strings</module>
<module>core-java-modules/core-java-httpclient</module>
<module>spring-core-6</module>
<module>data-structures</module>
<module>ddd-contexts</module>
<module>deeplearning4j</module>
<module>docker-modules</module>
<module>apache-httpclient-2</module>
<module>kubernetes-modules/kubernetes-spring</module>
@ -1163,11 +1152,13 @@
<module>spring-boot-modules/spring-boot-3-native</module>
<module>spring-boot-modules/spring-boot-3-observation</module>
<module>spring-boot-modules/spring-boot-3-test-pitfalls</module>
<module>spring-reactive-modules</module>
<module>spring-swagger-codegen/custom-validations-opeanpi-codegen</module>
<module>testing-modules/testing-assertions</module>
<module>persistence-modules/fauna</module>
<module>lightrun</module>
<module>tablesaw</module>
<module>geotools</module>
<!-- Modules from default-first -->

View File

@ -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)

View File

@ -26,4 +26,4 @@
</dependency>
</dependencies>
</project>
</project>

View File

@ -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<Book> 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<Book> response = restTemplate.exchange(
"https://api.bookstore.com",
HttpMethod.POST,
new HttpEntity<>(book, headers),
Book.class);
}
private static void execute() {
ResponseEntity<Book> 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<ResponseEntity<Book>>() {
@Override
public ResponseEntity<Book> 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;
}
}
}

View File

@ -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();
}
}