public class Result extends Object
builder and then you customize (or not) one or more HTTP attribute.
The following examples build the same output:
{
get("/", (req, rsp) -> {
rsp.status(200).send("Something");
});
get("/", req -> Results.with("Something", 200);
}
A result is also responsible for content negotiation (if required):
{
get("/", () -> {
Object model = ...;
return Results
.when("text/html", () -> Results.html("view").put("model", model))
.when("application/json", () -> model);
});
}
The example above will render a view when accept header is "text/html" or just send a text version of model when the accept header is "application/json".
Results
| Constructor and Description |
|---|
Result() |
| Modifier and Type | Method and Description |
|---|---|
<T> T |
get()
Get a result value.
|
<T> T |
get(List<MediaType> types)
Get a result value for the given types (accept header).
|
Result |
header(String name, Iterable<Object> values)
Sets a response header with the given name and value.
|
Result |
header(String name, Object... values)
Sets a response header with the given name and value.
|
Result |
header(String name, Object value)
Sets a response header with the given name and value.
|
Map<String,Object> |
headers() |
Optional<Object> |
ifGet()
Get a result value.
|
Optional<Object> |
ifGet(List<MediaType> types)
Get a result value for the given types (accept header).
|
Result |
set(Object content)
Set result content.
|
Optional<Status> |
status() |
Result |
status(int status)
Set response status.
|
Result |
status(Status status)
Set response status.
|
Optional<MediaType> |
type() |
Result |
type(MediaType type)
Set the content type of this content.
|
Result |
type(String type)
Set the content type of this content.
|
Result |
when(MediaType type, Supplier<Object> supplier)
Add a when clause for a custom result for the given media-type.
|
Result |
when(String type, Supplier<Object> supplier)
Add a when clause for a custom result for the given media-type.
|
@Nonnull public Result status(Status status)
status - A new response status to use.
@Nonnull public Result status(int status)
status - A new response status to use.
@Nonnull public Result type(MediaType type)
type - A content type.
@Nonnull public Result type(String type)
type - A content type.
@Nonnull public Result set(Object content)
content - A result content.
@Nonnull public Result when(String type, Supplier<Object> supplier)
type - A media type to test for.
supplier - An object supplier.
@Nonnull public Result when(MediaType type, Supplier<Object> supplier)
type - A media type to test for.
supplier - An object supplier.
@Nullable public <T> T get()
T - Value type.
null
@Nonnull public Optional<Object> ifGet(List<MediaType> types)
types - Accept header.
@Nullable public <T> T get(List<MediaType> types)
T - Value type.
types - Accept header.
null.
@Nonnull public Result header(String name, Object value)
name - Header's name.
value - Header's value.
@Nonnull public Result header(String name, Object... values)
name - Header's name.
values - Header's values.
Copyright © 2019. All rights reserved.