Skip to content

Commit 14a1b90

Browse files
authored
Merge pull request eugenp#4320 from softjake/master
BAEL-1764 Code update for Spring RestTemplate Exception Handling article.
2 parents 0f15f15 + 7404bb9 commit 14a1b90

7 files changed

Lines changed: 149 additions & 5 deletions

File tree

‎spring-rest-simple/pom.xml‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
</dependency>
3232
<dependency>
3333
<groupId>org.springframework.boot</groupId>
34-
<artifactId>spring-boot-test</artifactId>
34+
<artifactId>spring-boot-starter-test</artifactId>
35+
<scope>test</scope>
3536
</dependency>
3637

3738
<!-- Spring -->
@@ -240,7 +241,7 @@
240241
<artifactId>cargo-maven2-plugin</artifactId>
241242
<version>${cargo-maven2-plugin.version}</version>
242243
<configuration>
243-
<wait>true</wait>
244+
<!--<wait>true</wait> caused errors on commit-->
244245
<container>
245246
<containerId>tomcat8x</containerId>
246247
<type>embedded</type>

‎spring-rest-simple/src/main/java/org/baeldung/config/WebConfig.java‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
package org.baeldung.config;
22

3-
import java.text.SimpleDateFormat;
4-
import java.util.List;
5-
63
import org.baeldung.config.converter.KryoHttpMessageConverter;
74
import org.springframework.context.annotation.ComponentScan;
85
import org.springframework.context.annotation.Configuration;
@@ -19,6 +16,9 @@
1916
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
2017
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
2118

19+
import java.text.SimpleDateFormat;
20+
import java.util.List;
21+
2222
/*
2323
* Please note that main web configuration is in src/main/webapp/WEB-INF/api-servlet.xml
2424
*/
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package org.baeldung.web.exception;
2+
3+
public class NotFoundException extends RuntimeException {
4+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package org.baeldung.web.handler;
2+
3+
import org.baeldung.web.exception.NotFoundException;
4+
import org.springframework.http.HttpStatus;
5+
import org.springframework.http.client.ClientHttpResponse;
6+
import org.springframework.stereotype.Component;
7+
import org.springframework.web.client.ResponseErrorHandler;
8+
9+
import java.io.IOException;
10+
11+
@Component
12+
public class RestTemplateResponseErrorHandler
13+
implements ResponseErrorHandler {
14+
15+
@Override
16+
public boolean hasError(ClientHttpResponse httpResponse)
17+
throws IOException {
18+
19+
return (httpResponse
20+
.getStatusCode()
21+
.series() == HttpStatus.Series.CLIENT_ERROR || httpResponse
22+
.getStatusCode()
23+
.series() == HttpStatus.Series.SERVER_ERROR);
24+
}
25+
26+
@Override
27+
public void handleError(ClientHttpResponse httpResponse)
28+
throws IOException {
29+
30+
if (httpResponse
31+
.getStatusCode()
32+
.series() == HttpStatus.Series.SERVER_ERROR) {
33+
//Handle SERVER_ERROR
34+
} else if (httpResponse
35+
.getStatusCode()
36+
.series() == HttpStatus.Series.CLIENT_ERROR) {
37+
//Handle CLIENT_ERROR
38+
if (httpResponse.getStatusCode() == HttpStatus.NOT_FOUND) {
39+
throw new NotFoundException();
40+
}
41+
}
42+
}
43+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package org.baeldung.web.model;
2+
3+
public class Bar {
4+
private String id;
5+
private String name;
6+
7+
public String getId() {
8+
return id;
9+
}
10+
11+
public void setId(String id) {
12+
this.id = id;
13+
}
14+
15+
public String getName() {
16+
return name;
17+
}
18+
19+
public void setName(String name) {
20+
this.name = name;
21+
}
22+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package org.baeldung.web.service;
2+
3+
import org.baeldung.web.handler.RestTemplateResponseErrorHandler;
4+
import org.baeldung.web.model.Bar;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.boot.web.client.RestTemplateBuilder;
7+
import org.springframework.stereotype.Service;
8+
import org.springframework.web.client.RestTemplate;
9+
10+
@Service
11+
public class BarConsumerService {
12+
13+
private RestTemplate restTemplate;
14+
15+
@Autowired
16+
public BarConsumerService(RestTemplateBuilder restTemplateBuilder) {
17+
RestTemplate restTemplate = restTemplateBuilder
18+
.errorHandler(new RestTemplateResponseErrorHandler())
19+
.build();
20+
}
21+
22+
public Bar fetchBarById(String barId) {
23+
return restTemplate.getForObject("/bars/4242", Bar.class);
24+
}
25+
26+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package org.baeldung.web.handler;
2+
3+
import org.baeldung.web.exception.NotFoundException;
4+
import org.baeldung.web.model.Bar;
5+
import org.junit.Assert;
6+
import org.junit.Test;
7+
import org.junit.runner.RunWith;
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.boot.test.autoconfigure.web.client.RestClientTest;
10+
import org.springframework.boot.web.client.RestTemplateBuilder;
11+
import org.springframework.http.HttpMethod;
12+
import org.springframework.http.HttpStatus;
13+
import org.springframework.test.context.ContextConfiguration;
14+
import org.springframework.test.context.junit4.SpringRunner;
15+
import org.springframework.test.web.client.ExpectedCount;
16+
import org.springframework.test.web.client.MockRestServiceServer;
17+
import org.springframework.web.client.RestTemplate;
18+
19+
import static org.springframework.test.web.client.match.MockRestRequestMatchers.method;
20+
import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;
21+
import static org.springframework.test.web.client.response.MockRestResponseCreators.withStatus;
22+
23+
@RunWith(SpringRunner.class)
24+
@ContextConfiguration(classes = { NotFoundException.class, Bar.class })
25+
@RestClientTest
26+
public class RestTemplateResponseErrorHandlerIntegrationTest {
27+
28+
@Autowired private MockRestServiceServer server;
29+
@Autowired private RestTemplateBuilder builder;
30+
31+
@Test(expected = NotFoundException.class)
32+
public void givenRemoteApiCall_when404Error_thenThrowNotFound() {
33+
Assert.assertNotNull(this.builder);
34+
Assert.assertNotNull(this.server);
35+
36+
RestTemplate restTemplate = this.builder
37+
.errorHandler(new RestTemplateResponseErrorHandler())
38+
.build();
39+
40+
this.server
41+
.expect(ExpectedCount.once(), requestTo("/bars/4242"))
42+
.andExpect(method(HttpMethod.GET))
43+
.andRespond(withStatus(HttpStatus.NOT_FOUND));
44+
45+
Bar response = restTemplate.getForObject("/bars/4242", Bar.class);
46+
this.server.verify();
47+
}
48+
}

0 commit comments

Comments
 (0)