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, HttpServletResponse.setStatus(int status, String message)
is deprecated "due to ambiguous meaning of the message parameter."
Is there a preferred way to set the status message or "reason phrase" of the response? The sendError(int, String)
method doesn't do it.
EDIT: To clarify, I want to modify the HTTP status line, i.e. "HTTP/1.1 404 Not Found"
, not the body content. Specifically, I'd like to send responses like "HTTP/1.1 400 Missing customerNumber parameter"
.
After receiving and interpreting a request message, a server responds with an HTTP response message: A Status-line. Zero or more header (General|Response|Entity) fields followed by CRLF. An empty line (i.e., a line with nothing preceding the CRLF) indicating the end of the header fields. Optionally a message-body.
The most common HTTP request methods have a call shortcut (such as http. get and http. post), but you can make any type of HTTP request by setting the call field to http. request and specifying the type of request using the method field.
An HTTP response contains: A status line. A series of HTTP headers, or header fields. A message body, which is usually needed.
An HTTP message contains components that are appropriate to its type. The bit stream containing headers and body is parsed and represented within the message tree when an input request is received by an HTTPInput node, or when a response from a web service is received by the HTTPRequest node.
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.
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
See this page for some detail on the original vulnerability:
http://www.securityfocus.com/archive/1/archive/1/495021/100/0/threaded
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With