WsResponse
Overview
The WsResponse object represents a REST-WS response and is only valid while handling a REST-WS request. It allows manipulation of response headers, content type, status, and other properties essential for crafting HTTP responses.
The WsResponse object provides several properties to configure the HTTP response. For instance, characterEncoding sets the MIME charset, such as UTF-8. The contentType allows specifying the content type of the response, including optional character encoding. The localeLanguageTag retrieves the locale of the response, and the status property sets the HTTP status code for the response.
Methods like addCookie, addHeader, and setHeader are available for adding or setting cookies and headers in the response. Additional methods, such as sendError, allow setting error codes and messages, while setDateHeader and addDateHeader handle date-related headers. Other methods like getHeader and getHeaderNames facilitate inspecting headers in the response.
For overall REST-WS operations, refer to the Creating REST API section.
Properties Summarized
Sets the character encoding (MIME charset) of the response being sent to the client, for example, to UTF-8.
Sets the content type of the response being sent to the client, if the response has not been committed yet.
Methods Summarized
Returns a boolean indicating whether the named response header has already been set.
Properties Detailed
characterEncoding
Sets the character encoding (MIME charset) of the response being sent to the client, for example, to UTF-8. If the character encoding has already been set by #setContentType(String) or #setLocale, this method overrides it. Calling #setContentType(String) with the String of text/html and calling this method with the String of UTF-8 is equivalent with calling setContentType with the String of text/html; charset=UTF-8.
This method can be called repeatedly to change the character encoding. This method has no effect if it is called after getWriter has been called or after the response has been committed.
Containers must communicate the character encoding used for the servlet response's writer to the client if the protocol provides a way for doing so. In the case of HTTP, the character encoding is communicated as part of the Content-Type header for text media types. Note that the character encoding cannot be communicated via HTTP headers if the servlet does not specify a content type; however, it is still used to encode text written via the servlet response's writer.
Type String The character encoding (MIME charset) of the response.
contentType
Sets the content type of the response being sent to the client, if the response has not been committed yet. The given content type may include a character encoding specification, for example, text/html;charset=UTF-8. The response's character encoding is only set from the given content type if this method is called before getWriter is called.
This method may be called repeatedly to change content type and character encoding. This method has no effect if called after the response has been committed. It does not set the response's character encoding if it is called after getWriter has been called or after the response has been committed.
Containers must communicate the content type and the character encoding used for the servlet response's writer to the client if the protocol provides a way for doing so. In the case of HTTP, the Content-Type header is used.
Type String The MIME type of the content of this response.
localeLanguageTag
Returns the locale specified for this response using the #setLocale method. Calls made to setLocale after the response is committed have no effect. If no locale has been specified, the container's default locale is returned.
If the specified language tag contains any ill-formed subtags, the first such subtag and all following subtags are ignored. Compare to Locale.Builder#setLanguageTag(String) which throws an exception in this case.
The following conversions are performed:
The language code "und" is mapped to language "".
The language codes "he", "yi", and "id" are mapped to "iw", "ji", and "in" respectively. (This is the same canonicalization that's done in Locale's constructors.)
The portion of a private use subtag prefixed by "lvariant", if any, is removed and appended to the variant field in the result locale (without case normalization). If it is then empty, the private use subtag is discarded:
Locale loc; loc = Locale.forLanguageTag("en-US-x-lvariant-POSIX"); loc.getVariant(); // returns "POSIX" loc.getExtension('x'); // returns null
loc = Locale.forLanguageTag("de-POSIX-x-URP-lvariant-Abc-Def"); loc.getVariant(); // returns "POSIX_Abc_Def" loc.getExtension('x'); // returns "urp"
Locale.forLanguageTag("ja-JP-x-lvariant-JP").toLanguageTag(); // returns "ja-JP-u-ca-japanese-x-lvariant-JP" Locale.forLanguageTag("th-TH-x-lvariant-TH").toLanguageTag(); // returns "th-TH-u-nu-thai-x-lvariant-TH"
Last updated
Was this helpful?