Class RestResponse
- All Implemented Interfaces:
jakarta.servlet.http.HttpServletResponse
,jakarta.servlet.ServletResponse
The RestResponse
object is an extension of the
The primary methods on this class are:
RestResponse
- Methods for setting response headers:
addHeader(Header)
addHeader(String,String)
containsHeader(String)
getHeader(String)
setCharacterEncoding(String)
setContentType(String)
setHeader(Header)
setHeader(HttpPartSchema,String,Object)
setHeader(String,Object)
setHeader(String,String)
setMaxHeaderLength(int)
setSafeHeaders()
- Methods for setting response bodies:
- Other:
- Methods for setting response headers:
See Also:
-
Field Summary
Fields inherited from interface jakarta.servlet.http.HttpServletResponse
SC_ACCEPTED, SC_BAD_GATEWAY, SC_BAD_REQUEST, SC_CONFLICT, SC_CONTINUE, SC_CREATED, SC_EXPECTATION_FAILED, SC_FORBIDDEN, SC_FOUND, SC_GATEWAY_TIMEOUT, SC_GONE, SC_HTTP_VERSION_NOT_SUPPORTED, SC_INTERNAL_SERVER_ERROR, SC_LENGTH_REQUIRED, SC_METHOD_NOT_ALLOWED, SC_MOVED_PERMANENTLY, SC_MOVED_TEMPORARILY, SC_MULTIPLE_CHOICES, SC_NO_CONTENT, SC_NON_AUTHORITATIVE_INFORMATION, SC_NOT_ACCEPTABLE, SC_NOT_FOUND, SC_NOT_IMPLEMENTED, SC_NOT_MODIFIED, SC_OK, SC_PARTIAL_CONTENT, SC_PAYMENT_REQUIRED, SC_PRECONDITION_FAILED, SC_PROXY_AUTHENTICATION_REQUIRED, SC_REQUEST_ENTITY_TOO_LARGE, SC_REQUEST_TIMEOUT, SC_REQUEST_URI_TOO_LONG, SC_REQUESTED_RANGE_NOT_SATISFIABLE, SC_RESET_CONTENT, SC_SEE_OTHER, SC_SERVICE_UNAVAILABLE, SC_SWITCHING_PROTOCOLS, SC_TEMPORARY_REDIRECT, SC_UNAUTHORIZED, SC_UNSUPPORTED_MEDIA_TYPE, SC_USE_PROXY
-
Method Summary
Modifier and TypeMethodDescriptionvoid
Adds a response header with the given name and value.Adds a response header.void
Forces any content in the buffer to be written to the client.Shortcut for callinggetRequest().getAttributes() .Wrapper aroundServletResponseWrapper.getCharacterEncoding()
that converts the value to aCharset
.Returns the output that was set by callingsetContent(Object)
.<T> T
getContent
(Class<T> c) Returns this value cast to the specified class.Returns the schema of the response content.Returns access to the innerRestContext
of the class of this method.getDirectWriter
(String contentType) Convenience method meant to be used when rendering directly to a browser with no buffering.jakarta.servlet.http.HttpServletResponse
Returns the wrapped servlet request.Returns theContent-Type header stripped of the charset attribute if present.Equivalent toServletResponse.getOutputStream()
, except wraps the output stream if anEncoder
was found that matched theAccept-Encoding header.Equivalent toServletResponse.getWriter()
, except wraps the output stream if anEncoder
was found that matched theAccept-Encoding header and sets theContent-Encoding header to the appropriate value.Returns access to the innerRestOpContext
of this method.jakarta.servlet.ServletOutputStream
Returns a ServletOutputStream suitable for writing binary data in the response.boolean
Returnstrue ifgetOutputStream()
has been called.Returns the metadata about this response.Returns the matching serializer and media type for this response.Returns the writer to the response content.boolean
Returnstrue if the response contains output.boolean
isContentOfType
(Class<?> c) Returnstrue if this response object is of the specified type.sendPlainText
(String text) Sets the output to a plain-text message regardless of the content type.void
sendRedirect
(String uri) Redirects to the specified URI.setAttribute
(String name, Object value) Shortcut for callinggetRequest().setAttribute(String,Object) .setContent
(Object output) Sets the HTTP output on the response.setContentSchema
(HttpPartSchema schema) Specifies the schema for the response content.setDebug()
Shortcut for callingsetDebug( .true )Sets the"Debug" attribute to the specified boolean.Sets the"Exception" attribute to the specified throwable.Sets a header on the request.void
Sets a response header with the given name and value.Sets a response header.setHeader
(HttpPartSchema schema, String name, Object value) Sets a header on the request.setMaxHeaderLength
(int value) Specifies the maximum length for header values.Shortcut for callingsetNoTrace( .true )Sets the"NoTrace" attribute to the specified boolean.Sets metadata about this response.Enabled safe-header mode.Methods inherited from class jakarta.servlet.http.HttpServletResponseWrapper
addCookie, addDateHeader, addIntHeader, containsHeader, encodeRedirectURL, encodeURL, getHeader, getHeaderNames, getHeaders, getStatus, getTrailerFields, sendError, sendError, setDateHeader, setIntHeader, setStatus, setTrailerFields
Methods inherited from class jakarta.servlet.ServletResponseWrapper
getBufferSize, getCharacterEncoding, getContentType, getLocale, getResponse, isCommitted, isWrapperFor, isWrapperFor, reset, resetBuffer, setBufferSize, setCharacterEncoding, setContentLength, setContentLengthLong, setContentType, setLocale, setResponse
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface jakarta.servlet.ServletResponse
getBufferSize, getCharacterEncoding, getContentType, getLocale, isCommitted, reset, resetBuffer, setBufferSize, setCharacterEncoding, setContentLength, setContentLengthLong, setContentType, setLocale
-
Method Details
-
getContext
Returns access to the innerRestContext
of the class of this method.- Returns:
- The
RestContext
of this class. Nevernull .
-
getOpContext
Returns access to the innerRestOpContext
of this method.- Returns:
- The
RestOpContext
of this method. Nevernull .
-
setContent
Sets the HTTP output on the response.The object type can be anything allowed by the registered response handlers.
Calling this method is functionally equivalent to returning the object in the REST Java method.
Example:
@RestGet ("/example2/{personId}" )public void doGet(RestResponseres ,@Path UUIDpersonId ) { Personperson = getPersonById(personId );res .setOutput(person ); }Notes:
-
Calling this method with a
null value is NOT the same as not calling this method at all.
Anull output value means we want to serializenull as a response (e.g. as a JSONnull ).
Not calling this method or returning a value means you're handing the response yourself via the underlying stream or writer.
See Also:
- Parameters:
output
- The output to serialize to the connection.- Returns:
- This object.
-
Calling this method with a
-
getAttributes
Shortcut for callinggetRequest().getAttributes() .- Returns:
- The request attributes object.
-
setAttribute
Shortcut for callinggetRequest().setAttribute(String,Object) .- Parameters:
name
- The property name.value
- The property value.- Returns:
- This object.
-
getContent
Returns the output that was set by callingsetContent(Object)
.If it's null, then
setContent(Object)
wasn't called.
If it contains an empty, thensetObject( was called.null )
Otherwise,setContent(Object)
was called with a non-null value.- Returns:
- The output object, or
null ifsetContent(Object)
was never called.
-
hasContent
Returnstrue if the response contains output.This implies
setContent(Object)
has been called on this object.Note that this also returns
true even ifsetContent(Object)
was called with anull value as this means the response contains an output value ofnull as opposed to no value at all.- Returns:
true if the response contains output.
-
sendPlainText
Sets the output to a plain-text message regardless of the content type.- Parameters:
text
- The output text to send.- Returns:
- This object.
- Throws:
IOException
- If a problem occurred trying to write to the writer.
-
getNegotiatedOutputStream
Equivalent toServletResponse.getOutputStream()
, except wraps the output stream if anEncoder
was found that matched theAccept-Encoding header.- Returns:
- A negotiated output stream.
- Throws:
NotAcceptable
- If unsupported Accept-Encoding value specified.IOException
- Thrown by underlying stream.
-
getOutputStream
Returns a ServletOutputStream suitable for writing binary data in the response.The servlet container does not encode the binary data.
Calling
flush() on the ServletOutputStream commits the response. Either this method orgetWriter may be called to write the content, not both, except when reset has been called.- Specified by:
getOutputStream
in interfacejakarta.servlet.ServletResponse
- Overrides:
getOutputStream
in classjakarta.servlet.ServletResponseWrapper
- Returns:
- The stream.
- Throws:
IOException
- If stream could not be accessed.
-
getOutputStreamCalled
Returnstrue ifgetOutputStream()
has been called.- Returns:
true ifgetOutputStream()
has been called.
-
getWriter
Returns the writer to the response content.This methods bypasses any specified encoders and returns a regular unbuffered writer. Use the
getNegotiatedWriter()
method if you want to use the matched encoder (if any).- Specified by:
getWriter
in interfacejakarta.servlet.ServletResponse
- Overrides:
getWriter
in classjakarta.servlet.ServletResponseWrapper
- Returns:
- The writer.
- Throws:
IOException
- If writer could not be accessed.
-
getDirectWriter
Convenience method meant to be used when rendering directly to a browser with no buffering.Sets the header
"x-content-type-options=nosniff" so that output is rendered immediately on IE and Chrome without any buffering for content-type sniffing.This can be useful if you want to render a streaming 'console' on a web page.
- Parameters:
contentType
- The value to set as theContent-Type on the response.- Returns:
- The raw writer.
- Throws:
IOException
- Thrown by underlying stream.
-
getNegotiatedWriter
Equivalent toServletResponse.getWriter()
, except wraps the output stream if anEncoder
was found that matched theAccept-Encoding header and sets theContent-Encoding header to the appropriate value.- Returns:
- The negotiated writer.
- Throws:
NotAcceptable
- If unsupported charset in request header Accept-Charset.IOException
- Thrown by underlying stream.
-
getMediaType
Returns theContent-Type header stripped of the charset attribute if present.- Returns:
- The
media-type portion of theContent-Type header.
-
getCharset
Wrapper aroundServletResponseWrapper.getCharacterEncoding()
that converts the value to aCharset
.- Returns:
- The request character encoding converted to a
Charset
.
-
sendRedirect
Redirects to the specified URI.Relative URIs are always interpreted as relative to the context root. This is similar to how WAS handles redirect requests, and is different from how Tomcat handles redirect requests.
- Specified by:
sendRedirect
in interfacejakarta.servlet.http.HttpServletResponse
- Overrides:
sendRedirect
in classjakarta.servlet.http.HttpServletResponseWrapper
- Parameters:
uri
- The redirection URL.- Throws:
IOException
- If an input or output exception occurs
-
setHeader
Sets a response header with the given name and value.If the header had already been set, the new value overwrites the previous one.
The
HttpServletResponseWrapper.containsHeader(String)
method can be used to test for the presence of a header before setting its value.- Specified by:
setHeader
in interfacejakarta.servlet.http.HttpServletResponse
- Overrides:
setHeader
in classjakarta.servlet.http.HttpServletResponseWrapper
- Parameters:
name
- The header name.value
- The header value.
-
setHeader
public RestResponse setHeader(String name, Object value) throws SchemaValidationException, SerializeException Sets a header on the request.- Parameters:
name
- The header name.value
- The header value.- Can be any POJO.
- Converted to a string using the specified part serializer.
- Returns:
- This object.
- Throws:
SchemaValidationException
- Header failed schema validation.SerializeException
- Header could not be serialized.
-
setHeader
public RestResponse setHeader(HttpPartSchema schema, String name, Object value) throws SchemaValidationException, SerializeException Sets a header on the request.- Parameters:
schema
- The schema to use to serialize the header, ornull to use the default schema.name
- The header name.value
- The header value.- Can be any POJO.
- Converted to a string using the specified part serializer.
- Returns:
- This object.
- Throws:
SchemaValidationException
- Header failed schema validation.SerializeException
- Header could not be serialized.
-
setContentSchema
Specifies the schema for the response content.Used by schema-aware serializers such as
OpenApiSerializer
. Ignored by other serializers.- Parameters:
schema
- The content schema- Returns:
- This object.
-
setException
Sets the"Exception" attribute to the specified throwable.This exception is used by
CallLogger
for logging purposes.- Parameters:
t
- The attribute value.- Returns:
- This object.
-
setNoTrace
Sets the"NoTrace" attribute to the specified boolean.This flag is used by
CallLogger
and tells it not to log the current request.- Parameters:
b
- The attribute value.- Returns:
- This object.
-
setNoTrace
Shortcut for callingsetNoTrace( .true )- Returns:
- This object.
-
setDebug
Sets the"Debug" attribute to the specified boolean.This flag is used by
CallLogger
to help determine how a request should be logged.- Parameters:
b
- The attribute value.- Returns:
- This object.
- Throws:
IOException
- If bodies could not be cached.
-
setDebug
Shortcut for callingsetDebug( .true )- Returns:
- This object.
- Throws:
IOException
- If bodies could not be cached.
-
getResponseBeanMeta
Returns the metadata about this response.- Returns:
- The metadata about this response.
Nevernull .
-
setResponseBeanMeta
Sets metadata about this response.- Parameters:
rbm
- The metadata about this response.- Returns:
- This object.
-
isContentOfType
Returnstrue if this response object is of the specified type.- Parameters:
c
- The type to check against.- Returns:
true if this response object is of the specified type.
-
getContent
Returns this value cast to the specified class.- Type Parameters:
T
- The class to cast to.- Parameters:
c
- The class to cast to.- Returns:
- This value cast to the specified class, or
null if the object doesn't exist or isn't the specified type.
-
getHttpServletResponse
Returns the wrapped servlet request.- Returns:
- The wrapped servlet request.
-
flushBuffer
Forces any content in the buffer to be written to the client.A call to this method automatically commits the response, meaning the status code and headers will be written.
- Specified by:
flushBuffer
in interfacejakarta.servlet.ServletResponse
- Overrides:
flushBuffer
in classjakarta.servlet.ServletResponseWrapper
- Throws:
IOException
- If an I/O error occurred.
-
setSafeHeaders
Enabled safe-header mode.When enabled, invalid characters such as CTRL characters will be stripped from header values before they get set.
- Returns:
- This object.
-
setMaxHeaderLength
Specifies the maximum length for header values.Header values that exceed this length will get truncated.
- Parameters:
value
- The new value for this setting. The default is8096 .- Returns:
- This object.
-
addHeader
Adds a response header with the given name and value.This method allows response headers to have multiple values.
A no-op of either the name or value is
null .Note that per RFC2616, only headers defined as comma-delimited lists [i.e., #(values)] should be defined as multiple message header fields.
- Specified by:
addHeader
in interfacejakarta.servlet.http.HttpServletResponse
- Overrides:
addHeader
in classjakarta.servlet.http.HttpServletResponseWrapper
- Parameters:
name
- The header name.value
- The header value.
-
setHeader
Sets a response header.Any previous header values are removed.
Value is added at the end of the headers.
- Parameters:
header
- The header.- Returns:
- This object.
-
addHeader
Adds a response header.Any previous header values are preserved.
Value is added at the end of the headers.
If the header is a
BasicUriHeader
, the URI will be resolved using theRestRequest.getUriResolver()
object.If the header is a
SerializedHeader
and the serializer session is not set, it will be set to the one returned byRestRequest.getPartSerializerSession()
before serialization.Note that per RFC2616, only headers defined as comma-delimited lists [i.e., #(values)] should be defined as multiple message header fields.
- Parameters:
header
- The header.- Returns:
- This object.
-
getSerializerMatch
Returns the matching serializer and media type for this response.- Returns:
- The matching serializer, never
null .
-
getContentSchema
Returns the schema of the response content.- Returns:
- The schema of the response content, never
null .
-