Issue
I'm using RestTemplate client in Java and Spring Boot project and when I receive a response body from the server I have this code:
private RestTemplate oauth2RestTemplate;
private ParameterizedTypeReference<List<Employee>> parameterizedTypeReference;
....
ResponseEntity<List<Employee>> rtGetResponse = oauth2RestTemplate.exchange(uriComponents.toUri(), HttpMethod.GET, httpEntity, parameterizedTypeReference);
So in this case I will receive a list of employee.
Now I want to use this method but the response body from the server is void
, and I want to use exchange method but I don't know what to use instead of parameterizedTypeReference because the response is void. So I dont have a response body, I want only to catch the exception.
Is ok to do something like that?
try {
oauth2RestTemplate.exchange(uriComponents.toUri(), HttpMethod.GET, httpEntity, null);
} catch (HttpStatusCodeException e) {
switch (e.getStatusCode()) {
case BAD_REQUEST:
throw new BadRequestException(e);
case NOT_FOUND:
throw new NotFoundException(e);
...
}
}
Solution
You can use Void
class
try {
oauth2RestTemplate.exchange(uriComponents.toUri(), HttpMethod.GET, httpEntity, Void.class);
} catch (HttpStatusCodeException e) {
switch (e.getStatusCode()) {
case BAD_REQUEST:
throw new BadRequestException(e);
case NOT_FOUND:
throw new NotFoundException(e);
...
}
}
Answered By - Luke
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.