Skip to content

Commit d96ddd0

Browse files
authored
BAEL-9319: Parsing the Response Body When the HTTP Request Has Return… (#18628)
* BAEL-9319: Parsing the Response Body When the HTTP Request Has Return Status 401 in Java * BAEL-9319: Parsing the Response Body When the HTTP Request Has Return Status 401 in Java * BAEL-9319: Parsing the Response Body When the HTTP Request Has Return Status 401 in Java
1 parent da53793 commit d96ddd0

File tree

3 files changed

+92
-3
lines changed

3 files changed

+92
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.baeldung.resttemplate.web.exception;
2+
3+
public class UnauthorizedException extends RuntimeException {
4+
public UnauthorizedException(String message) {
5+
super(message);
6+
}
7+
}
Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package com.baeldung.resttemplate.web.service;
22

3+
import com.baeldung.resttemplate.web.exception.UnauthorizedException;
34
import com.baeldung.resttemplate.web.handler.RestTemplateResponseErrorHandler;
45
import com.baeldung.resttemplate.web.model.Bar;
56
import org.springframework.beans.factory.annotation.Autowired;
67
import org.springframework.boot.web.client.RestTemplateBuilder;
8+
import org.springframework.http.HttpStatus;
79
import org.springframework.stereotype.Service;
10+
import org.springframework.web.client.HttpStatusCodeException;
811
import org.springframework.web.client.RestTemplate;
912

1013
@Service
@@ -15,12 +18,20 @@ public class BarConsumerService {
1518
@Autowired
1619
public BarConsumerService(RestTemplateBuilder restTemplateBuilder) {
1720
restTemplate = restTemplateBuilder
18-
.errorHandler(new RestTemplateResponseErrorHandler())
19-
.build();
21+
.errorHandler(new RestTemplateResponseErrorHandler())
22+
.build();
2023
}
2124

2225
public Bar fetchBarById(String barId) {
23-
return restTemplate.getForObject("/bars/4242", Bar.class);
26+
try {
27+
return restTemplate.getForObject("/bars/" + barId, Bar.class);
28+
} catch (HttpStatusCodeException e) {
29+
if (HttpStatus.UNAUTHORIZED == e.getStatusCode()) {
30+
String responseBody = e.getResponseBodyAsString();
31+
throw new UnauthorizedException("Unauthorized access: " + responseBody);
32+
}
33+
throw e;
34+
}
2435
}
2536

2637
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.baeldung.resttemplate.web.service;
2+
3+
import com.baeldung.resttemplate.web.exception.UnauthorizedException;
4+
import com.baeldung.resttemplate.web.model.Bar;
5+
import org.junit.jupiter.api.BeforeEach;
6+
import org.junit.jupiter.api.Test;
7+
import org.junit.jupiter.api.extension.ExtendWith;
8+
import org.mockito.Mock;
9+
import org.mockito.junit.jupiter.MockitoExtension;
10+
import org.springframework.boot.web.client.RestTemplateBuilder;
11+
import org.springframework.http.HttpStatus;
12+
import org.springframework.web.client.HttpClientErrorException;
13+
import org.springframework.web.client.RestTemplate;
14+
15+
import static org.junit.jupiter.api.Assertions.*;
16+
import static org.mockito.ArgumentMatchers.any;
17+
import static org.mockito.ArgumentMatchers.eq;
18+
import static org.mockito.Mockito.when;
19+
20+
@ExtendWith(MockitoExtension.class)
21+
public class BarConsumerServiceUnitTest {
22+
23+
@Mock
24+
private RestTemplateBuilder restTemplateBuilder;
25+
26+
@Mock
27+
private RestTemplate restTemplate;
28+
29+
private BarConsumerService barConsumerService;
30+
31+
@BeforeEach
32+
public void setup() {
33+
when(restTemplateBuilder.errorHandler(any())).thenReturn(restTemplateBuilder);
34+
when(restTemplateBuilder.build()).thenReturn(restTemplate);
35+
36+
barConsumerService = new BarConsumerService(restTemplateBuilder);
37+
}
38+
39+
@Test
40+
public void givenValidId_whenFetchingBar_thenReturnsBar() {
41+
Bar expectedBar = new Bar();
42+
expectedBar.setId("123");
43+
expectedBar.setName("Test Bar");
44+
45+
when(restTemplate.getForObject(any(String.class), eq(Bar.class))).thenReturn(expectedBar);
46+
47+
Bar result = barConsumerService.fetchBarById("123");
48+
assertEquals(expectedBar, result);
49+
}
50+
51+
@Test
52+
public void givenUnauthorizedResponse_whenFetchingBar_thenThrowsUnauthorizedException() {
53+
String errorBody = "{\"error\": \"Invalid token\"}";
54+
HttpClientErrorException exception = HttpClientErrorException.create(
55+
HttpStatus.UNAUTHORIZED,
56+
"Unauthorized",
57+
null,
58+
errorBody.getBytes(),
59+
null
60+
);
61+
62+
when(restTemplate.getForObject(any(String.class), eq(Bar.class))).thenThrow(exception);
63+
64+
UnauthorizedException thrown = assertThrows(
65+
UnauthorizedException.class,
66+
() -> barConsumerService.fetchBarById("123")
67+
);
68+
69+
assertTrue(thrown.getMessage().contains(errorBody));
70+
}
71+
}

0 commit comments

Comments
 (0)