7.2.1 Customizing Parameter Binding
The previous example presented a trivial example that uses the parameters of a method to represent the body of a POST
request:
PetOperations.java
@Post
Single<Pet> save(@NotBlank String name, @Min(1L) int age);
The save
method when called will perform an HTTP POST
with the following JSON by default:
Example Produced JSON
{"name":"Dino", "age":10}
You may however want to customize what is sent as the body, the parameters, URI variables and so on. The @Client annotation is very flexible in this regard and supports the same HTTP Annotations as Micronaut’s HTTP server.
For example, the following defines a URI template and the name
parameter is used as part of the URI template, whilst @Body is used declare that the contents to send to the server are represented by the Pet
POJO:
PetOperations.java
@Post("/{name}")
Single<Pet> save(
@NotBlank String name, (1)
@Body @Valid Pet pet) (2)
1 | The name parameter, included as part of the URI, and declared @NotBlank |
2 | The pet parameter, used to encode the body and declared @Valid |
The following table summarizes the parameter annotations, their purpose, and provides an example:
Annotation | Description | Example |
---|---|---|
Allows to specify the parameter that is the body of the request |
| |
Allows specifying parameters that should be sent as cookies |
| |
Allows specifying parameters that should be sent as HTTP headers |
| |
Allows customizing the name of the URI parameter to bind from |
| |
Used to bind a parameter exclusively from a Path Variable. |
| |
Allows specifying parameters that should be set as request attributes |
|
Type Based Binding Parameters
Some parameters are recognized by their type instead of their annotation. The following table summarizes the parameter types, their purpose, and provides an example:
Type | Description | Example |
---|---|---|
Allows binding of basic authorization credentials |
|