Skip to content

Commit 1aaa12e

Browse files
author
jacques
committed
Corrections implementation.
1 parent 9a04bf2 commit 1aaa12e

3 files changed

Lines changed: 36 additions & 50 deletions

File tree

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package org.baeldung.web.handler;
22

33
import org.baeldung.web.exception.NotFoundException;
4-
import org.slf4j.Logger;
5-
import org.slf4j.LoggerFactory;
64
import org.springframework.http.HttpStatus;
75
import org.springframework.http.client.ClientHttpResponse;
86
import org.springframework.stereotype.Component;
@@ -11,13 +9,13 @@
119
import java.io.IOException;
1210

1311
@Component
14-
public class RestTemplateResponseErrorHandler implements ResponseErrorHandler {
15-
16-
private static final Logger LOGGER = LoggerFactory.getLogger(RestTemplateResponseErrorHandler.class);
17-
12+
public class RestTemplateResponseErrorHandler
13+
implements ResponseErrorHandler {
1814

1915
@Override
20-
public boolean hasError(ClientHttpResponse httpResponse) throws IOException {
16+
public boolean hasError(ClientHttpResponse httpResponse)
17+
throws IOException {
18+
2119
return (httpResponse
2220
.getStatusCode()
2321
.series() == HttpStatus.Series.CLIENT_ERROR || httpResponse
@@ -26,28 +24,20 @@ public boolean hasError(ClientHttpResponse httpResponse) throws IOException {
2624
}
2725

2826
@Override
29-
public void handleError(ClientHttpResponse httpResponse) throws IOException {
27+
public void handleError(ClientHttpResponse httpResponse)
28+
throws IOException {
29+
3030
if (httpResponse
3131
.getStatusCode()
3232
.series() == HttpStatus.Series.SERVER_ERROR) {
33-
this.handleServerError(httpResponse);
33+
//Handle SERVER_ERROR
3434
} else if (httpResponse
3535
.getStatusCode()
3636
.series() == HttpStatus.Series.CLIENT_ERROR) {
37-
this.handleClientError(httpResponse);
38-
}
39-
}
40-
41-
private void handleServerError(ClientHttpResponse httpResponse) {
42-
//Handle Server specific errors
43-
}
44-
45-
private void handleClientError(ClientHttpResponse httpResponse) throws IOException {
46-
//Handle Client specific errors
47-
if (httpResponse.getStatusCode() == HttpStatus.NOT_FOUND) {
48-
//Log details here...
49-
LOGGER.info("Throwing NotFoundException...");
50-
throw new NotFoundException();
37+
//Handle CLIENT_ERROR
38+
if (httpResponse.getStatusCode() == HttpStatus.NOT_FOUND) {
39+
throw new NotFoundException();
40+
}
5141
}
5242
}
5343
}

‎spring-rest-simple/src/main/java/org/baeldung/web/service/BarConsumerService.java‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,19 @@
77
import org.springframework.stereotype.Service;
88
import org.springframework.web.client.RestTemplate;
99

10-
1110
@Service
1211
public class BarConsumerService {
1312

13+
private RestTemplate restTemplate;
1414

15-
@Autowired RestTemplateBuilder restTemplateBuilder;
16-
17-
public Bar fetchBarById(String barId) {
15+
@Autowired
16+
public BarConsumerService(RestTemplateBuilder restTemplateBuilder) {
1817
RestTemplate restTemplate = restTemplateBuilder
1918
.errorHandler(new RestTemplateResponseErrorHandler())
2019
.build();
20+
}
21+
22+
public Bar fetchBarById(String barId) {
2123
return restTemplate.getForObject("/bars/4242", Bar.class);
2224
}
2325

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
package org.baeldung.web.handler;
22

3-
import org.baeldung.web.dto.Bazz;
43
import org.baeldung.web.exception.NotFoundException;
54
import org.baeldung.web.model.Bar;
65
import org.junit.Assert;
76
import org.junit.Test;
87
import org.junit.runner.RunWith;
9-
import org.slf4j.Logger;
10-
import org.slf4j.LoggerFactory;
118
import org.springframework.beans.factory.annotation.Autowired;
129
import org.springframework.boot.test.autoconfigure.web.client.RestClientTest;
1310
import org.springframework.boot.web.client.RestTemplateBuilder;
@@ -24,31 +21,28 @@
2421
import static org.springframework.test.web.client.response.MockRestResponseCreators.withStatus;
2522

2623
@RunWith(SpringRunner.class)
27-
@ContextConfiguration(classes = {NotFoundException.class, Bar.class})
24+
@ContextConfiguration(classes = { NotFoundException.class, Bar.class })
2825
@RestClientTest
2926
public class RestTemplateResponseErrorHandlerIntegrationTest {
3027

31-
private static final Logger LOGGER = LoggerFactory.getLogger(RestTemplateResponseErrorHandler.class);
28+
@Autowired private MockRestServiceServer server;
29+
@Autowired private RestTemplateBuilder builder;
3230

33-
@Autowired private MockRestServiceServer server;
34-
@Autowired private RestTemplateBuilder builder;
31+
@Test(expected = NotFoundException.class)
32+
public void givenRemoteApiCall_when404Error_thenThrowNotFound() {
33+
Assert.assertNotNull(this.builder);
34+
Assert.assertNotNull(this.server);
3535

36+
RestTemplate restTemplate = this.builder
37+
.errorHandler(new RestTemplateResponseErrorHandler())
38+
.build();
3639

37-
@Test(expected = NotFoundException.class)
38-
public void givenCallToRemoteApi_when404ErrorReceived_throwNotFoundException() {
39-
Assert.assertNotNull(this.builder);
40-
Assert.assertNotNull(this.server);
40+
this.server
41+
.expect(ExpectedCount.once(), requestTo("/bars/4242"))
42+
.andExpect(method(HttpMethod.GET))
43+
.andRespond(withStatus(HttpStatus.NOT_FOUND));
4144

42-
RestTemplate restTemplate = this.builder
43-
.errorHandler(new RestTemplateResponseErrorHandler())
44-
.build();
45-
46-
this.server
47-
.expect(ExpectedCount.once(), requestTo("/bars/4242"))
48-
.andExpect(method(HttpMethod.GET))
49-
.andRespond(withStatus(HttpStatus.NOT_FOUND));
50-
51-
Bar response = restTemplate.getForObject("/bars/4242", Bar.class);
52-
this.server.verify();
53-
}
45+
Bar response = restTemplate.getForObject("/bars/4242", Bar.class);
46+
this.server.verify();
47+
}
5448
}

0 commit comments

Comments
 (0)