BAEL-6911 A Guide to RestClient in Spring Boot (#14893)

This commit is contained in:
mdabrowski-eu 2023-10-02 13:53:53 +02:00 committed by GitHub
parent 071d0ca2e9
commit 2ed008decc
5 changed files with 266 additions and 1 deletions

View File

@ -41,6 +41,27 @@
<artifactId>mockserver-netty</artifactId> <artifactId>mockserver-netty</artifactId>
<version>${mockserver.version}</version> <version>${mockserver.version}</version>
</dependency> </dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>${jupiter.version}</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${jupiter.version}</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${jupiter.version}</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>${jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency> <dependency>
<groupId>org.mock-server</groupId> <groupId>org.mock-server</groupId>
<artifactId>mockserver-client-java</artifactId> <artifactId>mockserver-client-java</artifactId>
@ -187,8 +208,46 @@
<maven-surefire-plugin.version>3.0.0-M7</maven-surefire-plugin.version> <maven-surefire-plugin.version>3.0.0-M7</maven-surefire-plugin.version>
<start-class>com.baeldung.sample.TodoApplication</start-class> <start-class>com.baeldung.sample.TodoApplication</start-class>
<mockserver.version>5.14.0</mockserver.version> <mockserver.version>5.14.0</mockserver.version>
<spring-boot.version>3.1.0</spring-boot.version> <spring-boot.version>3.2.0-SNAPSHOT</spring-boot.version>
<lombok-mapstruct-binding.version>0.2.0</lombok-mapstruct-binding.version> <lombok-mapstruct-binding.version>0.2.0</lombok-mapstruct-binding.version>
<jupiter.version>5.10.0</jupiter.version>
</properties> </properties>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<releases>
<enabled>false</enabled>
</releases>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<releases>
<enabled>false</enabled>
</releases>
</pluginRepository>
</pluginRepositories>
</project> </project>

View File

@ -0,0 +1,34 @@
package com.baeldung.restclient;
import java.util.Objects;
public class Article {
Integer id;
String title;
public Article(Integer id, String title) {
this.id = id;
this.title = title;
}
public Integer getId() {
return id;
}
public String getTitle() {
return title;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Article article = (Article) o;
return Objects.equals(id, article.id) && Objects.equals(title, article.title);
}
@Override
public int hashCode() {
return Objects.hash(id, title);
}
}

View File

@ -0,0 +1,45 @@
package com.baeldung.restclient;
import org.springframework.web.bind.annotation.*;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
@RestController
@RequestMapping("/articles")
public class ArticleController {
Map<Integer, Article> database = new HashMap<>();
@GetMapping
public Collection<Article> getArticles() {
return database.values();
}
@GetMapping("/{id}")
public Article getArticle(@PathVariable Integer id) {
return database.get(id);
}
@PostMapping
public void createArticle(@RequestBody Article article) {
database.put(article.getId(), article);
}
@PutMapping("/{id}")
public void updateArticle(@PathVariable Integer id, @RequestBody Article article) {
assert Objects.equals(id, article.getId());
database.remove(id);
database.put(id, article);
}
@DeleteMapping("/{id}")
public void deleteArticle(@PathVariable Integer id) {
database.remove(id);
}
@DeleteMapping()
public void deleteArticles() {
database.clear();
}
}

View File

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

View File

@ -0,0 +1,114 @@
package com.baeldung.restclient;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.server.LocalServerPort;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.MediaType;
import org.springframework.web.client.RestClient;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class RestClientIntegrationTest {
@LocalServerPort
private int port;
private String uriBase;
RestClient restClient = RestClient.create();
@BeforeAll
public void setup() {
uriBase = "http://localhost:" + port;
}
@AfterEach
public void teardown() {
restClient.delete()
.uri(uriBase + "/articles")
.retrieve()
.toBodilessEntity();
}
@Test
void shouldGetArticlesAndReturnString() {
String articlesAsString = restClient.get()
.uri(uriBase + "/articles")
.retrieve()
.body(String.class);
assertThat(articlesAsString).isEqualTo("[]");
}
@Test
void shouldPostAndGetArticles() {
Article article = new Article(1, "How to use RestClient");
restClient.post()
.uri(uriBase + "/articles")
.contentType(MediaType.APPLICATION_JSON)
.body(article)
.retrieve()
.toBodilessEntity();
List<Article> articles = restClient.get()
.uri(uriBase + "/articles")
.retrieve()
.body(new ParameterizedTypeReference<>() {});
assertThat(articles).isEqualTo(List.of(article));
}
@Test
void shouldPostAndPutAndGetArticles() {
Article article = new Article(1, "How to use RestClient");
restClient.post()
.uri(uriBase + "/articles")
.contentType(MediaType.APPLICATION_JSON)
.body(article)
.retrieve()
.toBodilessEntity();
Article articleChanged = new Article(1, "How to use RestClient even better");
restClient.put()
.uri(uriBase + "/articles/1")
.contentType(MediaType.APPLICATION_JSON)
.body(articleChanged)
.retrieve()
.toBodilessEntity();
List<Article> articles = restClient.get()
.uri(uriBase + "/articles")
.retrieve()
.body(new ParameterizedTypeReference<>() {});
assertThat(articles).isEqualTo(List.of(articleChanged));
}
@Test
void shouldPostAndDeleteArticles() {
Article article = new Article(1, "How to use RestClient");
restClient.post()
.uri(uriBase + "/articles")
.contentType(MediaType.APPLICATION_JSON)
.body(article)
.retrieve()
.toBodilessEntity();
restClient.delete()
.uri(uriBase + "/articles")
.retrieve()
.toBodilessEntity();
List<Article> articles = restClient.get()
.uri(uriBase + "/articles")
.retrieve()
.body(new ParameterizedTypeReference<>() {});
assertThat(articles).isEqualTo(List.of());
}
}