From 82c09ad76e02d57da34565409e14f0bbcd1613ea Mon Sep 17 00:00:00 2001 From: Hardik Singh Behl Date: Tue, 27 Jan 2026 13:36:17 +0530 Subject: [PATCH 1/2] delete classes specific to restclient demonstration --- .../java/com/baeldung/restclient/Article.java | 44 ----- .../restclient/ArticleController.java | 61 ------- .../restclient/ArticleNotFoundException.java | 6 - .../InvalidArticleResponseException.java | 6 - .../InterceptingClientHttpUnitTest.java | 22 --- .../restclient/RestClientLiveTest.java | 168 ------------------ 6 files changed, 307 deletions(-) delete mode 100644 spring-boot-modules/spring-boot-3/src/main/java/com/baeldung/restclient/Article.java delete mode 100644 spring-boot-modules/spring-boot-3/src/main/java/com/baeldung/restclient/ArticleController.java delete mode 100644 spring-boot-modules/spring-boot-3/src/main/java/com/baeldung/restclient/ArticleNotFoundException.java delete mode 100644 spring-boot-modules/spring-boot-3/src/main/java/com/baeldung/restclient/InvalidArticleResponseException.java delete mode 100644 spring-boot-modules/spring-boot-3/src/test/java/com/baeldung/restclient/InterceptingClientHttpUnitTest.java delete mode 100644 spring-boot-modules/spring-boot-3/src/test/java/com/baeldung/restclient/RestClientLiveTest.java diff --git a/spring-boot-modules/spring-boot-3/src/main/java/com/baeldung/restclient/Article.java b/spring-boot-modules/spring-boot-3/src/main/java/com/baeldung/restclient/Article.java deleted file mode 100644 index 892cf3b5aeb7..000000000000 --- a/spring-boot-modules/spring-boot-3/src/main/java/com/baeldung/restclient/Article.java +++ /dev/null @@ -1,44 +0,0 @@ -package com.baeldung.restclient; - -import java.util.Objects; - -public class Article { - Integer id; - String title; - - public Article() {} - - public Article(Integer id, String title) { - this.id = id; - this.title = title; - } - - public Integer getId() { - return id; - } - - public String getTitle() { - return title; - } - - public void setId(Integer id) { - this.id = id; - } - - public void setTitle(String title) { - this.title = 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); - } -} diff --git a/spring-boot-modules/spring-boot-3/src/main/java/com/baeldung/restclient/ArticleController.java b/spring-boot-modules/spring-boot-3/src/main/java/com/baeldung/restclient/ArticleController.java deleted file mode 100644 index c2d87a558cec..000000000000 --- a/spring-boot-modules/spring-boot-3/src/main/java/com/baeldung/restclient/ArticleController.java +++ /dev/null @@ -1,61 +0,0 @@ -package com.baeldung.restclient; - -import java.util.Collection; -import java.util.HashMap; -import java.util.Map; -import java.util.Objects; - -import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.DeleteMapping; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.PutMapping; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; - -@RestController -@RequestMapping("/articles") -public class ArticleController { - Map database = new HashMap<>(); - - @GetMapping - public ResponseEntity> getArticles() { - Collection
values = database.values(); - if (values.isEmpty()) { - return ResponseEntity.noContent().build(); - } - return ResponseEntity.ok(values); - } - - @GetMapping("/{id}") - public ResponseEntity
getArticle(@PathVariable("id") Integer id) { - Article article = database.get(id); - if (article == null) { - return ResponseEntity.notFound().build(); - } - return ResponseEntity.ok(article); - } - - @PostMapping - public void createArticle(@RequestBody Article article) { - database.put(article.getId(), article); - } - - @PutMapping("/{id}") - public void updateArticle(@PathVariable("id") 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(); - } -} diff --git a/spring-boot-modules/spring-boot-3/src/main/java/com/baeldung/restclient/ArticleNotFoundException.java b/spring-boot-modules/spring-boot-3/src/main/java/com/baeldung/restclient/ArticleNotFoundException.java deleted file mode 100644 index cdd13b83303e..000000000000 --- a/spring-boot-modules/spring-boot-3/src/main/java/com/baeldung/restclient/ArticleNotFoundException.java +++ /dev/null @@ -1,6 +0,0 @@ -package com.baeldung.restclient; - -public class ArticleNotFoundException extends RuntimeException { - public ArticleNotFoundException() { - } -} diff --git a/spring-boot-modules/spring-boot-3/src/main/java/com/baeldung/restclient/InvalidArticleResponseException.java b/spring-boot-modules/spring-boot-3/src/main/java/com/baeldung/restclient/InvalidArticleResponseException.java deleted file mode 100644 index 26ca75036e4a..000000000000 --- a/spring-boot-modules/spring-boot-3/src/main/java/com/baeldung/restclient/InvalidArticleResponseException.java +++ /dev/null @@ -1,6 +0,0 @@ -package com.baeldung.restclient; - -public class InvalidArticleResponseException extends RuntimeException { - public InvalidArticleResponseException() { - } -} diff --git a/spring-boot-modules/spring-boot-3/src/test/java/com/baeldung/restclient/InterceptingClientHttpUnitTest.java b/spring-boot-modules/spring-boot-3/src/test/java/com/baeldung/restclient/InterceptingClientHttpUnitTest.java deleted file mode 100644 index e41c4b078ce9..000000000000 --- a/spring-boot-modules/spring-boot-3/src/test/java/com/baeldung/restclient/InterceptingClientHttpUnitTest.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.baeldung.restclient; - -import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; - -import org.junit.jupiter.api.Test; -import org.springframework.http.client.ClientHttpRequestInterceptor; - -class InterceptingClientHttpUnitTest { - - @Test - void updateRequestAttribute() throws Exception { - String attrName = "attr1"; - String attrValue = "value1"; - - assertDoesNotThrow(() -> { - ClientHttpRequestInterceptor interceptor = (request, body, execution) -> { - request.getAttributes().put(attrName, attrValue); - return execution.execute(request, body); - }; - }); - } -} diff --git a/spring-boot-modules/spring-boot-3/src/test/java/com/baeldung/restclient/RestClientLiveTest.java b/spring-boot-modules/spring-boot-3/src/test/java/com/baeldung/restclient/RestClientLiveTest.java deleted file mode 100644 index 0284d780a9fa..000000000000 --- a/spring-boot-modules/spring-boot-3/src/test/java/com/baeldung/restclient/RestClientLiveTest.java +++ /dev/null @@ -1,168 +0,0 @@ -package com.baeldung.restclient; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatThrownBy; - -import java.util.List; - -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.boot.test.web.server.LocalServerPort; -import org.springframework.core.ParameterizedTypeReference; -import org.springframework.http.HttpStatusCode; -import org.springframework.http.MediaType; -import org.springframework.http.ResponseEntity; -import org.springframework.test.context.TestPropertySource; -import org.springframework.web.client.RestClient; - -import com.fasterxml.jackson.core.type.TypeReference; -import com.fasterxml.jackson.databind.ObjectMapper; - -@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) -@TestPropertySource(locations="classpath:connectiondetails/application-r2dbc.properties") -public class RestClientLiveTest { - - @LocalServerPort - private int port; - private String uriBase; - RestClient restClient = RestClient.create(); - - @Autowired - ObjectMapper objectMapper; - - @BeforeEach - 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
articles = restClient.get() - .uri(uriBase + "/articles") - .retrieve() - .body(new ParameterizedTypeReference<>() {}); - - assertThat(articles).isEqualTo(List.of(article)); - } - - @Test - void shouldPostAndGetArticlesWithExchange() { - assertThatThrownBy(this::getArticlesWithExchange).isInstanceOf(ArticleNotFoundException.class); - - Article article = new Article(1, "How to use RestClient"); - restClient.post() - .uri(uriBase + "/articles") - .contentType(MediaType.APPLICATION_JSON) - .body(article) - .retrieve() - .toBodilessEntity(); - - List
articles = getArticlesWithExchange(); - - assertThat(articles).isEqualTo(List.of(article)); - } - - private List
getArticlesWithExchange() { - return restClient.get() - .uri(uriBase + "/articles") - .exchange((request, response) -> { - if (response.getStatusCode().isSameCodeAs(HttpStatusCode.valueOf(204))) { - throw new ArticleNotFoundException(); - } else if (response.getStatusCode().isSameCodeAs(HttpStatusCode.valueOf(200))) { - return objectMapper.readValue(response.getBody(), new TypeReference<>() {}); - } else { - throw new InvalidArticleResponseException(); - } - }); - } - - @Test - void shouldPostAndGetArticlesWithErrorHandling() { - assertThatThrownBy(() -> { - restClient.get() - .uri(uriBase + "/articles/1234") - .retrieve() - .onStatus(status -> status.value() == 404, (request, response) -> { throw new ArticleNotFoundException(); }) - .body(new ParameterizedTypeReference() {}); - }).isInstanceOf(ArticleNotFoundException.class); - } - - @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
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(); - - ResponseEntity entity = restClient.get() - .uri(uriBase + "/articles") - .accept(MediaType.APPLICATION_JSON) - .retrieve() - .toBodilessEntity(); - - assertThat(entity.getStatusCode()).isEqualTo(HttpStatusCode.valueOf(204)); - } -} From 400f7be07254d6506b39eb8f565a6a1fc7562920 Mon Sep 17 00:00:00 2001 From: Hardik Singh Behl Date: Tue, 27 Jan 2026 13:37:19 +0530 Subject: [PATCH 2/2] rename main class --- .../{RestClientApplication.java => Application.java} | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) rename spring-boot-modules/spring-boot-3/src/main/java/com/baeldung/restclient/{RestClientApplication.java => Application.java} (68%) diff --git a/spring-boot-modules/spring-boot-3/src/main/java/com/baeldung/restclient/RestClientApplication.java b/spring-boot-modules/spring-boot-3/src/main/java/com/baeldung/restclient/Application.java similarity index 68% rename from spring-boot-modules/spring-boot-3/src/main/java/com/baeldung/restclient/RestClientApplication.java rename to spring-boot-modules/spring-boot-3/src/main/java/com/baeldung/restclient/Application.java index c411a8f74aab..f2660a60dfe1 100644 --- a/spring-boot-modules/spring-boot-3/src/main/java/com/baeldung/restclient/RestClientApplication.java +++ b/spring-boot-modules/spring-boot-3/src/main/java/com/baeldung/restclient/Application.java @@ -4,10 +4,9 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication -public class RestClientApplication { +class Application { public static void main(String[] args) { - SpringApplication.run(RestClientApplication.class, args); + SpringApplication.run(Application.class, args); } - -} +} \ No newline at end of file