public class ResponseBuilder
extends java.lang.Object
Modifier and Type | Class and Description |
---|---|
static class |
ResponseBuilder.InvalidResponseException
Thrown when
ResponseBuilder is missing some parameters. |
Modifier and Type | Method and Description |
---|---|
Response |
build()
Finish building.
|
ResponseBuilder |
redirectTo(java.lang.String url)
Utility method for building redirects.
|
ResponseBuilder |
setContent(byte[] bytes) |
ResponseBuilder |
setContent(java.lang.String utf8String) |
ResponseBuilder |
setContent(java.lang.String content,
java.nio.charset.Charset charset) |
ResponseBuilder |
setContent(WebResource resource) |
ResponseBuilder |
setHeader(java.lang.String header,
java.lang.Object value)
Set HTTP Response header.
|
ResponseBuilder |
setJSONContent(java.lang.Object objectToSerialize)
Set content as serialized JSON object.
|
ResponseBuilder |
setJSONContent(java.lang.String json) |
ResponseBuilder |
setMimeType(java.lang.String mimeType)
Set MIME Type of the Response.
|
ResponseBuilder |
setStatus(int code)
Set HTTP Status code.
|
public ResponseBuilder setMimeType(java.lang.String mimeType)
mimeType
- MIME type of the Response https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_typesfor common MIME types.
public ResponseBuilder setStatus(int code)
Default status code is 200 (OK) if not set.
code
- 1xx, 2xx, 3xx, 4xx, 5xx, https://developer.mozilla.org/en-US/docs/Web/HTTP/Statuspublic ResponseBuilder setHeader(java.lang.String header, java.lang.Object value)
header
- Key of the header. https://developer.mozilla.org/en-US/docs/Web/HTTP/Headersvalue
- Value for the header.public ResponseBuilder redirectTo(java.lang.String url)
url
- URL to redirect the client to with 302 Found.public ResponseBuilder setContent(WebResource resource)
public ResponseBuilder setContent(byte[] bytes)
public ResponseBuilder setContent(java.lang.String utf8String)
public ResponseBuilder setContent(java.lang.String content, java.nio.charset.Charset charset)
public ResponseBuilder setJSONContent(java.lang.Object objectToSerialize)
objectToSerialize
- Object to serialize into JSON with Gson. If the object is a String it is assumed to be valid JSON.public ResponseBuilder setJSONContent(java.lang.String json)
public Response build()
ResponseBuilder.InvalidResponseException
- if content was not defined (not even empty byte array).ResponseBuilder.InvalidResponseException
- if content has bytes, but MIME-type is not defined.ResponseBuilder.InvalidResponseException
- if status code is outside range 100-599.to set MIME-type.