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
This commit is contained in:
committed by
GitHub
parent
6e9091f686
commit
69fad866d9
-1
@@ -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
|
||||
|
||||
+12
-20
@@ -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();
|
||||
|
||||
+1
-1
@@ -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;
|
||||
|
||||
+3
-4
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+4
-4
@@ -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<String> 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);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user