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
@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:
HTTP response parts:
These annotation can be used on method parameters or on the parameter types themselves, or a combination of both.
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:
@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:
// Inline fluent assertion and retrieval.
String foo = request
.getHeader("Foo")
.assertString().contains("bar")
.get();
See also
In This Section
📄️ 3.3.4.1. Part Marshallers
Juneau comes with three basic marshaller types for serializing and parsing Header, Query, Form, and Path parts:
📄️ 3.3.4.2. HTTP Part Annotations
The following annotations allow for defining part schemas based on the OpenAPI standard.
📄️ 3.3.4.3. Default Parts
By default, HTTP parts that don't have value (such as missing query parameters) end up with null values:
📄️ 3.3.4.4. Request Beans
The @Request annotation can used to define proxy
📄️ 3.3.4.5. @Response Beans
The Response annotation can be used to define beans that
📄️ 3.3.4.6. HTTP Part Validation
HTTP parts can be automatically validated against their schema definitions using format validation and Jakarta Bean Validation constraints.