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 SummaryFields inherited from interface jakarta.servlet.http.HttpServletResponseSC_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_MISDIRECTED_REQUEST, 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_PERMANENT_REDIRECT, 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_UNPROCESSABLE_CONTENT, SC_UNSUPPORTED_MEDIA_TYPE, SC_UPGRADE_REQUIRED, SC_USE_PROXY
- 
Method SummaryModifier and TypeMethodDescriptionvoidAdds a response header with the given name and value.Adds a response header.voidForces 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> TgetContent(Class<T> c) Returns this value cast to the specified class.Returns the schema of the response content.Returns access to the innerRestContextof 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.HttpServletResponseReturns the wrapped servlet request.Returns theContent-Type header stripped of the charset attribute if present.Equivalent toServletResponse.getOutputStream(), except wraps the output stream if anEncoderwas found that matched theAccept-Encoding header.Equivalent toServletResponse.getWriter(), except wraps the output stream if anEncoderwas found that matched theAccept-Encoding header and sets theContent-Encoding header to the appropriate value.Returns access to the innerRestOpContextof this method.jakarta.servlet.ServletOutputStreamReturns a ServletOutputStream suitable for writing binary data in the response.booleanReturnstrue 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.booleanReturnstrue if the response contains output.booleanisContentOfType(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.voidsendRedirect(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.voidSets 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.HttpServletResponseWrapperaddCookie, addDateHeader, addIntHeader, containsHeader, encodeRedirectURL, encodeURL, getHeader, getHeaderNames, getHeaders, getStatus, getTrailerFields, sendError, sendError, sendRedirect, sendRedirect, sendRedirect, setDateHeader, setIntHeader, setStatus, setTrailerFieldsMethods inherited from class jakarta.servlet.ServletResponseWrappergetBufferSize, getCharacterEncoding, getContentType, getLocale, getResponse, isCommitted, isWrapperFor, isWrapperFor, reset, resetBuffer, setBufferSize, setCharacterEncoding, setCharacterEncoding, setContentLength, setContentLengthLong, setContentType, setLocale, setResponseMethods inherited from class java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface jakarta.servlet.ServletResponsegetBufferSize, getCharacterEncoding, getContentType, getLocale, isCommitted, reset, resetBuffer, setBufferSize, setCharacterEncoding, setCharacterEncoding, setContentLength, setContentLengthLong, setContentType, setLocale
- 
Method Details- 
getContextReturns access to the innerRestContextof the class of this method.- Returns:
- The RestContextof this class. Nevernull .
 
- 
getOpContextReturns access to the innerRestOpContextof this method.- Returns:
- The RestOpContextof this method. Nevernull .
 
- 
setContentSets 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 
- 
getAttributesShortcut for callinggetRequest().getAttributes() .- Returns:
- The request attributes object.
 
- 
setAttributeShortcut for callinggetRequest().setAttribute(String,Object) .- Parameters:
- name- The property name.
- value- The property value.
- Returns:
- This object.
 
- 
getContentReturns 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.
 
- 
hasContentReturnstrue 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.
 
- 
sendPlainTextSets 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.
 
- 
getNegotiatedOutputStreamEquivalent toServletResponse.getOutputStream(), except wraps the output stream if anEncoderwas found that matched theAccept-Encoding header.- Returns:
- A negotiated output stream.
- Throws:
- NotAcceptable- If unsupported Accept-Encoding value specified.
- IOException- Thrown by underlying stream.
 
- 
getOutputStreamReturns 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:
- getOutputStreamin interface- jakarta.servlet.ServletResponse
- Overrides:
- getOutputStreamin class- jakarta.servlet.ServletResponseWrapper
- Returns:
- The stream.
- Throws:
- IOException- If stream could not be accessed.
 
- 
getOutputStreamCalledReturnstrue ifgetOutputStream()has been called.- Returns:
- true if- getOutputStream()has been called.
 
- 
getWriterReturns 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:
- getWriterin interface- jakarta.servlet.ServletResponse
- Overrides:
- getWriterin class- jakarta.servlet.ServletResponseWrapper
- Returns:
- The writer.
- Throws:
- IOException- If writer could not be accessed.
 
- 
getDirectWriterConvenience 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 the- Content-Type on the response.
- Returns:
- The raw writer.
- Throws:
- IOException- Thrown by underlying stream.
 
- 
getNegotiatedWriterEquivalent toServletResponse.getWriter(), except wraps the output stream if anEncoderwas 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.
 
- 
getMediaTypeReturns theContent-Type header stripped of the charset attribute if present.- Returns:
- The media-type portion of theContent-Type header.
 
- 
getCharsetWrapper aroundServletResponseWrapper.getCharacterEncoding()that converts the value to aCharset.- Returns:
- The request character encoding converted to a Charset.
 
- 
sendRedirectRedirects 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:
- sendRedirectin interface- jakarta.servlet.http.HttpServletResponse
- Overrides:
- sendRedirectin class- jakarta.servlet.http.HttpServletResponseWrapper
- Parameters:
- uri- The redirection URL.
- Throws:
- IOException- If an input or output exception occurs
 
- 
setHeaderSets 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:
- setHeaderin interface- jakarta.servlet.http.HttpServletResponse
- Overrides:
- setHeaderin class- jakarta.servlet.http.HttpServletResponseWrapper
- Parameters:
- name- The header name.
- value- The header value.
 
- 
setHeaderpublic 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.
 
- 
setHeaderpublic 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, or- null 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.
 
- 
setContentSchemaSpecifies 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.
 
- 
setExceptionSets the"Exception" attribute to the specified throwable.This exception is used by CallLoggerfor logging purposes.- Parameters:
- t- The attribute value.
- Returns:
- This object.
 
- 
setNoTraceSets the"NoTrace" attribute to the specified boolean.This flag is used by CallLoggerand tells it not to log the current request.- Parameters:
- b- The attribute value.
- Returns:
- This object.
 
- 
setNoTraceShortcut for callingsetNoTrace( .true )- Returns:
- This object.
 
- 
setDebugSets the"Debug" attribute to the specified boolean.This flag is used by CallLoggerto help determine how a request should be logged.- Parameters:
- b- The attribute value.
- Returns:
- This object.
- Throws:
- IOException- If bodies could not be cached.
 
- 
setDebugShortcut for callingsetDebug( .true )- Returns:
- This object.
- Throws:
- IOException- If bodies could not be cached.
 
- 
getResponseBeanMetaReturns the metadata about this response.- Returns:
- The metadata about this response.
   
 Nevernull .
 
- 
setResponseBeanMetaSets metadata about this response.- Parameters:
- rbm- The metadata about this response.
- Returns:
- This object.
 
- 
isContentOfTypeReturnstrue 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.
 
- 
getContentReturns 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.
 
- 
getHttpServletResponseReturns the wrapped servlet request.- Returns:
- The wrapped servlet request.
 
- 
flushBufferForces 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:
- flushBufferin interface- jakarta.servlet.ServletResponse
- Overrides:
- flushBufferin class- jakarta.servlet.ServletResponseWrapper
- Throws:
- IOException- If an I/O error occurred.
 
- 
setSafeHeadersEnabled safe-header mode.When enabled, invalid characters such as CTRL characters will be stripped from header values before they get set. - Returns:
- This object.
 
- 
setMaxHeaderLengthSpecifies 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 is- 8096 .
- Returns:
- This object.
 
- 
addHeaderAdds 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:
- addHeaderin interface- jakarta.servlet.http.HttpServletResponse
- Overrides:
- addHeaderin class- jakarta.servlet.http.HttpServletResponseWrapper
- Parameters:
- name- The header name.
- value- The header value.
 
- 
setHeaderSets 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.
 
- 
addHeaderAdds 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 SerializedHeaderand 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.
 
- 
getSerializerMatchReturns the matching serializer and media type for this response.- Returns:
- The matching serializer, never null .
 
- 
getContentSchemaReturns the schema of the response content.- Returns:
- The schema of the response content, never null .
 
 
-