Skip to main content

HTTP parts in REST servers

In previous examples we showed the ability to pass in annotated parameters on RestOp-annotated methods to parse standard HTTP parts.

For the HTTP part serializers and parsers themselves, see HTTP Part Serializers and Parsers.

Example​

Example
@RestGet("/example1/{a1}/*")
public String doGetExample1(
@Path("a1") String a1,
@Query("p1") int p1,
@HasQuery("p2") boolean hasP3,
@Path("/*") String remainder,
@Header("Accept-Language") String lang
) {
// Do something with all of those
}

Annotations are provided for both request and response HTTP parts.

The annotations used for defining the schema for request HTTP parts are:

HTTP request parts:​

RequestContentHeaderFormDataQueryPathHasFormDataHasQuery

HTTP response parts:​

ResponseContentHeaderStatusCode

These annotation can be used on method parameters or on the parameter types themselves, or a combination of both.

note

When defined on types, annotations are aggregated from parent to child with child values taking precedence. When defined on both, annotations are aggregated with values on parameters taking precedence.

HTTP part APIs​

Request HTTP parts can also be retrieved programmatically through the following classes that can be passed in as parameters or access through RestRequest bean:

RequestHeaderList RequestQueryParamList RequestFormParamList RequestPathParamList
Example
@RestPost(...)
public Object myMethod(RequestHeaderList headers) {

// Add a default value.
headers.addDefault("ETag", DEFAULT_UUID);

// Get a header value as a POJO.
UUID etag = headers.get("ETag").as(UUID.class).get();

// Get a header as a standard HTTP part.
ContentType contentType = headers.get(ContentType.class).orElse(ContentType.TEXT_XML);
}

Built in to these APIs are various convenience methods such as converting parts to different types or inline fluent assertions: