question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Errorneous usage of HttpServletResponse in the rest endpoints

See original GitHub issue

The 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:closed
  • Created 6 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
g051051commented, Nov 27, 2020

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.

1reaction
g051051commented, Nov 27, 2020
  1. Local build was successful. Deployment of dirigible-desktop-all to local Apache Tomcat successful.
  2. I tested the following samples:
  • Hello World: produced “Hello World” output.
  • Bookstore Application: worked as expected.
  • HTML5 (AngularJS): worked as expected.
  • Message Listener: The IDE shows an error the “exports is not defined” for the handler.js file, but the output is logged as expected. I think this is an existing error in the sample.
  • Websocket Service: Same “exports is not defined” error in the IDE, but the output is logged as expected.
  1. http://localhost:8080/services/v4/web/not-existing-project/not-existing-page/ produces and error page with: “Resource not found: not-existing-project/not-existing-page/index.html”.
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found