Errorneous usage of HttpServletResponse in the rest endpoints
See original GitHub issueThe various rest services follow the pattern:
if (some_condition) {
sendErrorXXX(response, message); //send the error response
return Response.status(Status.ERROR_STATUS).build(); //sending the error response again
}
The various “sendErrorXXXX” methods in AbstractRestService are unnecessary and duplicate the behavior of the returned Response object. Even more - it’s an error to use both. The proper way to send any kind of response in JAX-RS is to use the ResposneBuilder. JAX-RS endpoints should avoid touching the HttpServletResponse directly.
Issue Analytics
- State:
- Created 6 years ago
- Comments:7 (7 by maintainers)
Top Results From Across the Web
Reactive Spring does not support HttpServletRequest as ...
The class you were using to get the servlet response was internal and should not be used in application code. This can be...
Read more >HttpServletResponse (Java(TM) EE 7 Specification APIs)
Extends the ServletResponse interface to provide HTTP-specific functionality in sending a response. For example, it has methods to access HTTP headers and ...
Read more >HttpServletResponse Interface with Example - Java Guides
The HttpServletResponse interface enables a servlet to formulate an HTTP response to a client. The response object encapsulates all information to be returned ......
Read more >Servlet Exception and Error Handling Example Tutorial
Let's see how our servlet container responds to 404 error. If we send request for an invalid URL, we get response HTML like...
Read more >Error handling for a Spring-based REST API - Microflash
In this post, we'll explore some of the ways to customize error responses returned by a REST API. We'll also cover some usecases...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I actually took the opposite tack, and changed the implementation of the methods to all be in the form of:
/** * Create error response not found. * * @param message * the message * @return the error response */ protected Response createErrorResponseNotFound(String message) { return Response.serverError().status(Status.NOT_FOUND).entity(message).build(); }
then updated all the references to just do something like:
return createErrorResponseNotFound(path);
I don’t see this as worse, but I can do it the other way if it’s somehow preferable.