Corrections implementation.
This commit is contained in:
parent
9a04bf2c2c
commit
1aaa12eca6
|
@ -1,8 +1,6 @@
|
|||
package org.baeldung.web.handler;
|
||||
|
||||
import org.baeldung.web.exception.NotFoundException;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.client.ClientHttpResponse;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
@ -11,13 +9,13 @@ import org.springframework.web.client.ResponseErrorHandler;
|
|||
import java.io.IOException;
|
||||
|
||||
@Component
|
||||
public class RestTemplateResponseErrorHandler implements ResponseErrorHandler {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(RestTemplateResponseErrorHandler.class);
|
||||
|
||||
public class RestTemplateResponseErrorHandler
|
||||
implements ResponseErrorHandler {
|
||||
|
||||
@Override
|
||||
public boolean hasError(ClientHttpResponse httpResponse) throws IOException {
|
||||
public boolean hasError(ClientHttpResponse httpResponse)
|
||||
throws IOException {
|
||||
|
||||
return (httpResponse
|
||||
.getStatusCode()
|
||||
.series() == HttpStatus.Series.CLIENT_ERROR || httpResponse
|
||||
|
@ -26,28 +24,20 @@ public class RestTemplateResponseErrorHandler implements ResponseErrorHandler {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void handleError(ClientHttpResponse httpResponse) throws IOException {
|
||||
public void handleError(ClientHttpResponse httpResponse)
|
||||
throws IOException {
|
||||
|
||||
if (httpResponse
|
||||
.getStatusCode()
|
||||
.series() == HttpStatus.Series.SERVER_ERROR) {
|
||||
this.handleServerError(httpResponse);
|
||||
//Handle SERVER_ERROR
|
||||
} else if (httpResponse
|
||||
.getStatusCode()
|
||||
.series() == HttpStatus.Series.CLIENT_ERROR) {
|
||||
this.handleClientError(httpResponse);
|
||||
}
|
||||
}
|
||||
|
||||
private void handleServerError(ClientHttpResponse httpResponse) {
|
||||
//Handle Server specific errors
|
||||
}
|
||||
|
||||
private void handleClientError(ClientHttpResponse httpResponse) throws IOException {
|
||||
//Handle Client specific errors
|
||||
if (httpResponse.getStatusCode() == HttpStatus.NOT_FOUND) {
|
||||
//Log details here...
|
||||
LOGGER.info("Throwing NotFoundException...");
|
||||
throw new NotFoundException();
|
||||
//Handle CLIENT_ERROR
|
||||
if (httpResponse.getStatusCode() == HttpStatus.NOT_FOUND) {
|
||||
throw new NotFoundException();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,17 +7,19 @@ import org.springframework.boot.web.client.RestTemplateBuilder;
|
|||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
|
||||
@Service
|
||||
public class BarConsumerService {
|
||||
|
||||
private RestTemplate restTemplate;
|
||||
|
||||
@Autowired RestTemplateBuilder restTemplateBuilder;
|
||||
|
||||
public Bar fetchBarById(String barId) {
|
||||
@Autowired
|
||||
public BarConsumerService(RestTemplateBuilder restTemplateBuilder) {
|
||||
RestTemplate restTemplate = restTemplateBuilder
|
||||
.errorHandler(new RestTemplateResponseErrorHandler())
|
||||
.build();
|
||||
}
|
||||
|
||||
public Bar fetchBarById(String barId) {
|
||||
return restTemplate.getForObject("/bars/4242", Bar.class);
|
||||
}
|
||||
|
||||
|
|
|
@ -1,13 +1,10 @@
|
|||
package org.baeldung.web.handler;
|
||||
|
||||
import org.baeldung.web.dto.Bazz;
|
||||
import org.baeldung.web.exception.NotFoundException;
|
||||
import org.baeldung.web.model.Bar;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.web.client.RestClientTest;
|
||||
import org.springframework.boot.web.client.RestTemplateBuilder;
|
||||
|
@ -24,31 +21,28 @@ import static org.springframework.test.web.client.match.MockRestRequestMatchers.
|
|||
import static org.springframework.test.web.client.response.MockRestResponseCreators.withStatus;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@ContextConfiguration(classes = {NotFoundException.class, Bar.class})
|
||||
@ContextConfiguration(classes = { NotFoundException.class, Bar.class })
|
||||
@RestClientTest
|
||||
public class RestTemplateResponseErrorHandlerIntegrationTest {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(RestTemplateResponseErrorHandler.class);
|
||||
@Autowired private MockRestServiceServer server;
|
||||
@Autowired private RestTemplateBuilder builder;
|
||||
|
||||
@Autowired private MockRestServiceServer server;
|
||||
@Autowired private RestTemplateBuilder builder;
|
||||
@Test(expected = NotFoundException.class)
|
||||
public void givenRemoteApiCall_when404Error_thenThrowNotFound() {
|
||||
Assert.assertNotNull(this.builder);
|
||||
Assert.assertNotNull(this.server);
|
||||
|
||||
RestTemplate restTemplate = this.builder
|
||||
.errorHandler(new RestTemplateResponseErrorHandler())
|
||||
.build();
|
||||
|
||||
@Test(expected = NotFoundException.class)
|
||||
public void givenCallToRemoteApi_when404ErrorReceived_throwNotFoundException() {
|
||||
Assert.assertNotNull(this.builder);
|
||||
Assert.assertNotNull(this.server);
|
||||
this.server
|
||||
.expect(ExpectedCount.once(), requestTo("/bars/4242"))
|
||||
.andExpect(method(HttpMethod.GET))
|
||||
.andRespond(withStatus(HttpStatus.NOT_FOUND));
|
||||
|
||||
RestTemplate restTemplate = this.builder
|
||||
.errorHandler(new RestTemplateResponseErrorHandler())
|
||||
.build();
|
||||
|
||||
this.server
|
||||
.expect(ExpectedCount.once(), requestTo("/bars/4242"))
|
||||
.andExpect(method(HttpMethod.GET))
|
||||
.andRespond(withStatus(HttpStatus.NOT_FOUND));
|
||||
|
||||
Bar response = restTemplate.getForObject("/bars/4242", Bar.class);
|
||||
this.server.verify();
|
||||
}
|
||||
Bar response = restTemplate.getForObject("/bars/4242", Bar.class);
|
||||
this.server.verify();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue