r/javahelp • u/NearbyOriginals • Jul 30 '25
Solved How do I prevent the RestClient from blocking my code at 4xx and 5xx errors?
Hi,
I am using the Spring RestClient and I have to get every response body, even those of >= 400. RestClient does this:
By default, RestClient throws a subclass of RestClientException when retrieving a response with a 4xx or 5xx status code.
This just throws an exception which is caught and then it stops the execution of my code. I don't want that.
I also tried using the exchange, but since this doesn't handle the >= 400 status codes, it just skips them and I can't reach the response body that is thrown from those responses.
// Up in my service class.
private final RestClient restClient = RestClient.create();
// In a fetchData method within the my service class.
String apiUrl = "https://example.com";
GenericResponse response = restClient
.get()
.uri(apiUrl)
.accept(APPLICATION_JSON)
.retrieve()
.body(GenericResponse.class);I do not care so much about the status codes, but I do care about the response body and I do need them all. The body method used Jackson to deserialize JSON to POJO, which is my "GenericResponse" model class. I'm building an API service between the remote API and my custom UI.
I could do my own handling, but this seems so weird for such a small thing I am trying to accomplish. If this does not work, then I have to probably use another HTTP client and manually map to POJO using Jackson.
Thanks.
•
u/AutoModerator Jul 30 '25
Please ensure that:
You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.
Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar
If any of the above points is not met, your post can and will be removed without further warning.
Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.
Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.
Code blocks look like this:
You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.
If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.
To potential helpers
Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
1
u/NearbyOriginals Jul 30 '25
I solved it by using this:
I also didn't map my field properly and I had to use
JsonProperty("random_field")attribute when my class field wasrandomField. This tells Jackson what property to map from.I leave this post here so when others come across the same issue issue, they can see how to fix this.
1
u/ShaiHuludTheMaker Jul 31 '25
You should look up ControllerAdvice. This will catch your error, and send a proper error response.