How to properly send an HTTP message to the client

后端 未结 7 1136
一生所求
一生所求 2021-02-05 00:14

I\'m working on a RESTful web service in Java. I need a good way to send error messages to the client if something\'s wrong.

According to the Javadoc, HttpServlet

相关标签:
7条回答
  • 2021-02-05 00:30

    I'm not quite familiar with the 'best practices' around REST. But I know the concept is based on HTTP and how it is supposed to work out naturally. So how about using a mime type and simple text inside the body for an application error, like 'application/myapp-exception' and some 'Bla bla'? You can provide a client library for that.

    I would not use HTTP response codes for application errors. Because I like to know what's failing: whether it is my application or my HTTP server.

    (I hope, I'll see some best practice advices here, too.)

    0 讨论(0)
  • 2021-02-05 00:32

    I don't think any RESTful client would expect to look at the reason phrase to figure out what went wrong; most RESTful services I've seen/used will send the standard status info and an expanded message in the body of the response. sendError(int, String) is ideal for that situation.

    0 讨论(0)
  • 2021-02-05 00:44

    It's not really clear what you are trying to accomplish. My first thought was the sendError but you say that does not do what you want... have you looked at creating a set of "error responses", meaning specific xml or JSON content (or whatever you are using as a transfer language) that contains the error message or code and any other useful information?

    I did something like that for Spring-mvc based RESTful services a while back and it worked well but you have to pretty much catch and handle every exception to keep the client from getting a generic 500 message or something. The Spring Exception Resolvers worked well for that.

    Hope this helps... if not, maybe a little more clarity on what you are trying to accomplish. Sorry if I am being dense and missing something obvious.

    0 讨论(0)
  • 2021-02-05 00:51

    I think the sendError should do it, but your application server may be failing... IBM WebSphere 3.5 failed on me a long time ago while Tomcat would propagate the message just fine; see JavaServer Pages (JSP) and JSTL - Error page: preserve header "HTTP/1.x 400 My message"? on the Sun forums.

    Eventually I used the following workaround, but this is kind of JSP specific, and may in fact be old:

    <%@ page isErrorPage="true" %>
    <%
        // This attribute is NOT set when calling HttpResponse#setStatus and then
        // explicitely incuding this error page using RequestDispatcher#include()
        // So: only set by HttpResponse#sendError()
        Integer origStatus = 
            (Integer)request.getAttribute("javax.servlet.error.status_code");
        if(origStatus != null) {
            String origMessage = 
                (String)request.getAttribute("javax.servlet.error.message");
            if(origMessage != null) {
                response.reset();
                response.setContentType("text/html");
                // deprecated, but works:
                response.setStatus(origStatus.intValue(), origMessage); 
                // would yield recursive error:
                // response.sendError(origStatus, origMessage); 
            }
        }
    %>
    

    And if you happen to test with Internet Explorer: disable "Show friendly HTTP error messages". (When not disabling that, IE has some odd requirement of some minimum length of the HTML content which, if not met, would —or will— make IE show its own error message instead. See also the registry key HKEY_LOCAL_MACHINE\Software\Microsoft\Internet Explorer\Main\ErrorThresholds at Microsoft's Description of Hypertext Transport Protocol Error Messages.)

    0 讨论(0)
  • 2021-02-05 00:53

    If you're using Tomcat, see the setting org.apache.coyote.USE_CUSTOM_STATUS_MSG_IN_HEADER:

    http://tomcat.apache.org/tomcat-5.5-doc/config/systemprops.html

    • If this is true custom HTTP status messages will be used within HTTP headers. Users must ensure that any such message is ISO-8859-1 encoded, particularly if user provided input is included in the message, to prevent a possible XSS vulnerability. If not specified the default value of false will be used.

    See this page for some detail on the original vulnerability:

    http://www.securityfocus.com/archive/1/archive/1/495021/100/0/threaded

    0 讨论(0)
  • 2021-02-05 00:53

    After your clarification, I tried this in Tomcat. Executing

    response.sendError(HttpServletResponse.SC_BAD_REQUEST, "message goes here");
    

    returns

    HTTP/1.1 400 message goes here
    

    as the first line in the response.

    There must be a problem with the servlet container you are using.

    0 讨论(0)
提交回复
热议问题