From 69fad866d949c9e1dad46e2001c46647ec25bcd9 Mon Sep 17 00:00:00 2001 From: Kasra Madadipouya Date: Tue, 23 Jan 2024 16:54:18 +0100 Subject: [PATCH] JAVA-30632 Upgrade spring-resttemplate module to Spring Boot 3 (#15713) * JAVA-30632 upgrade Spring rest template module to Spring Boot 3 * JAVA-30632 fix broken tests --- .../spring-resttemplate/.gitignore | 13 --- .../spring-resttemplate/pom.xml | 87 ++++--------------- .../configuration/SpringConfig.java | 1 - .../RestTemplateResponseErrorHandler.java | 32 +++---- .../web/controller/MyFooController.java | 2 +- .../mock/EmployeeServiceUnitTest.java | 7 +- .../RestTemplateBasicLiveTest.java | 8 +- 7 files changed, 36 insertions(+), 114 deletions(-) delete mode 100644 spring-web-modules/spring-resttemplate/.gitignore diff --git a/spring-web-modules/spring-resttemplate/.gitignore b/spring-web-modules/spring-resttemplate/.gitignore deleted file mode 100644 index 83c05e60c8..0000000000 --- a/spring-web-modules/spring-resttemplate/.gitignore +++ /dev/null @@ -1,13 +0,0 @@ -*.class - -#folders# -/target -/neoDb* -/data -/src/main/webapp/WEB-INF/classes -*/META-INF/* - -# Packaged files # -*.jar -*.war -*.ear \ No newline at end of file diff --git a/spring-web-modules/spring-resttemplate/pom.xml b/spring-web-modules/spring-resttemplate/pom.xml index 84c1add07e..3a03f3aafc 100644 --- a/spring-web-modules/spring-resttemplate/pom.xml +++ b/spring-web-modules/spring-resttemplate/pom.xml @@ -10,71 +10,20 @@ com.baeldung - parent-boot-2 + parent-boot-3 0.0.1-SNAPSHOT - ../../parent-boot-2 + ../../parent-boot-3 - - org.springframework.boot - spring-boot-starter-actuator - - - org.springframework.boot - spring-boot-starter-thymeleaf - - - org.springframework.boot - spring-boot-devtools - org.springframework.boot spring-boot-starter-web org.springframework.boot - spring-boot-starter-test - - - au.com.dius - pact-jvm-provider-junit5_2.12 - ${pact.version} - - - au.com.dius - pact-jvm-consumer-junit5_2.12 - ${pact.version} - test - - - - org.springframework - spring-web - - - commons-logging - commons-logging - - - - - org.springframework - spring-webmvc - - - org.springframework - spring-oxm - - - - com.fasterxml.jackson.core - jackson-databind - - - com.fasterxml.jackson.dataformat - jackson-dataformat-xml + spring-boot-starter-actuator com.thoughtworks.xstream @@ -100,14 +49,15 @@ - org.springframework - spring-test - - - org.mockito - mockito-junit-jupiter - ${mockito.version} + org.springframework.boot + spring-boot-starter-test test + + + org.junit.vintage + junit-vintage-engine + + @@ -120,14 +70,13 @@ + + org.springframework.boot + spring-boot-maven-plugin + org.apache.maven.plugins maven-compiler-plugin - ${maven.version} - - ${source.version} - ${target.version} - org.apache.maven.plugins @@ -254,16 +203,12 @@ - 1.4.9 + 1.4.20 1.6.1 3.0.4 4.12.0 - 3.6.3 - 1.8 - 1.8 - 3.7.0 \ No newline at end of file diff --git a/spring-web-modules/spring-resttemplate/src/main/java/com/baeldung/resttemplate/configuration/SpringConfig.java b/spring-web-modules/spring-resttemplate/src/main/java/com/baeldung/resttemplate/configuration/SpringConfig.java index 7866a4abcd..f6a618fc12 100644 --- a/spring-web-modules/spring-resttemplate/src/main/java/com/baeldung/resttemplate/configuration/SpringConfig.java +++ b/spring-web-modules/spring-resttemplate/src/main/java/com/baeldung/resttemplate/configuration/SpringConfig.java @@ -7,7 +7,6 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.DependsOn; -import org.springframework.web.client.RestTemplate; @Configuration @EnableAutoConfiguration diff --git a/spring-web-modules/spring-resttemplate/src/main/java/com/baeldung/resttemplate/web/handler/RestTemplateResponseErrorHandler.java b/spring-web-modules/spring-resttemplate/src/main/java/com/baeldung/resttemplate/web/handler/RestTemplateResponseErrorHandler.java index 02260438c7..fe7e1239c5 100644 --- a/spring-web-modules/spring-resttemplate/src/main/java/com/baeldung/resttemplate/web/handler/RestTemplateResponseErrorHandler.java +++ b/spring-web-modules/spring-resttemplate/src/main/java/com/baeldung/resttemplate/web/handler/RestTemplateResponseErrorHandler.java @@ -2,40 +2,32 @@ package com.baeldung.resttemplate.web.handler; import java.io.IOException; -import com.baeldung.resttemplate.web.exception.NotFoundException; import org.springframework.http.HttpStatus; import org.springframework.http.client.ClientHttpResponse; import org.springframework.stereotype.Component; import org.springframework.web.client.HttpClientErrorException; import org.springframework.web.client.ResponseErrorHandler; +import com.baeldung.resttemplate.web.exception.NotFoundException; + @Component -public class RestTemplateResponseErrorHandler - implements ResponseErrorHandler { +public class RestTemplateResponseErrorHandler implements ResponseErrorHandler { @Override - public boolean hasError(ClientHttpResponse httpResponse) - throws IOException { - - return (httpResponse - .getStatusCode() - .series() == HttpStatus.Series.CLIENT_ERROR || httpResponse - .getStatusCode() - .series() == HttpStatus.Series.SERVER_ERROR); + public boolean hasError(ClientHttpResponse httpResponse) throws IOException { + return httpResponse.getStatusCode() + .is5xxServerError() || httpResponse.getStatusCode() + .is4xxClientError(); } @Override - public void handleError(ClientHttpResponse httpResponse) - throws IOException { - - if (httpResponse - .getStatusCode() - .series() == HttpStatus.Series.SERVER_ERROR) { + public void handleError(ClientHttpResponse httpResponse) throws IOException { + if (httpResponse.getStatusCode() + .is5xxServerError()) { //Handle SERVER_ERROR throw new HttpClientErrorException(httpResponse.getStatusCode()); - } else if (httpResponse - .getStatusCode() - .series() == HttpStatus.Series.CLIENT_ERROR) { + } else if (httpResponse.getStatusCode() + .is4xxClientError()) { //Handle CLIENT_ERROR if (httpResponse.getStatusCode() == HttpStatus.NOT_FOUND) { throw new NotFoundException(); diff --git a/spring-web-modules/spring-resttemplate/src/main/java/com/baeldung/sampleapp/web/controller/MyFooController.java b/spring-web-modules/spring-resttemplate/src/main/java/com/baeldung/sampleapp/web/controller/MyFooController.java index 5c385edb07..c1a4825fc0 100644 --- a/spring-web-modules/spring-resttemplate/src/main/java/com/baeldung/sampleapp/web/controller/MyFooController.java +++ b/spring-web-modules/spring-resttemplate/src/main/java/com/baeldung/sampleapp/web/controller/MyFooController.java @@ -4,7 +4,7 @@ import java.util.Collection; import java.util.HashMap; import java.util.Map; -import javax.servlet.http.HttpServletResponse; +import jakarta.servlet.http.HttpServletResponse; import org.springframework.http.HttpStatus; import org.springframework.stereotype.Controller; diff --git a/spring-web-modules/spring-resttemplate/src/test/java/com/baeldung/mock/EmployeeServiceUnitTest.java b/spring-web-modules/spring-resttemplate/src/test/java/com/baeldung/mock/EmployeeServiceUnitTest.java index 4c6e2fa5ca..97d49bd335 100644 --- a/spring-web-modules/spring-resttemplate/src/test/java/com/baeldung/mock/EmployeeServiceUnitTest.java +++ b/spring-web-modules/spring-resttemplate/src/test/java/com/baeldung/mock/EmployeeServiceUnitTest.java @@ -1,5 +1,7 @@ package com.baeldung.mock; +import static org.junit.jupiter.api.Assertions.assertEquals; + import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -18,8 +20,6 @@ import com.baeldung.resttemplate.web.model.Employee; @ExtendWith(MockitoExtension.class) public class EmployeeServiceUnitTest { - private static final Logger logger = LoggerFactory.getLogger(EmployeeServiceUnitTest.class); - @Mock private RestTemplate restTemplate; @@ -34,7 +34,6 @@ public class EmployeeServiceUnitTest { Employee employee = empService.getEmployee("E001"); - Assertions.assertEquals(emp, employee); + assertEquals(emp, employee); } - } diff --git a/spring-web-modules/spring-resttemplate/src/test/java/com/baeldung/resttemplate/RestTemplateBasicLiveTest.java b/spring-web-modules/spring-resttemplate/src/test/java/com/baeldung/resttemplate/RestTemplateBasicLiveTest.java index a433ac73c3..0c5d473291 100644 --- a/spring-web-modules/spring-resttemplate/src/test/java/com/baeldung/resttemplate/RestTemplateBasicLiveTest.java +++ b/spring-web-modules/spring-resttemplate/src/test/java/com/baeldung/resttemplate/RestTemplateBasicLiveTest.java @@ -1,6 +1,6 @@ package com.baeldung.resttemplate; -import static org.apache.commons.codec.binary.Base64.encodeBase64; +import java.util.Base64; import static com.baeldung.client.Consts.APPLICATION_PORT; import static org.junit.jupiter.api.Assertions.fail; @@ -32,7 +32,7 @@ import org.springframework.web.client.RestTemplate; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.dataformat.xml.XmlMapper; +import org.springframework.web.client.RestTemplate; import com.google.common.base.Charsets; // This test needs RestTemplateConfigurationApplication to be up and running @@ -62,7 +62,7 @@ public class RestTemplateBasicLiveTest { final RestTemplate template = new RestTemplate(); final ResponseEntity response = template.getForEntity(fooResourceUrl + "/1", String.class); - final ObjectMapper mapper = new XmlMapper(); + final ObjectMapper mapper = new ObjectMapper(); final JsonNode root = mapper.readTree(response.getBody()); final JsonNode name = root.path("name"); Assertions.assertNotNull(name.asText()); @@ -243,7 +243,7 @@ public class RestTemplateBasicLiveTest { private String getBase64EncodedLogPass() { final String logPass = "user1:user1Pass"; - final byte[] authHeaderBytes = encodeBase64(logPass.getBytes(Charsets.US_ASCII)); + final byte[] authHeaderBytes = Base64.getEncoder().encode(logPass.getBytes(Charsets.US_ASCII)); return new String(authHeaderBytes, Charsets.US_ASCII); }