Sending JSON
The previous example sends plain text. To send JSON, pass the object to encode to JSON (whether that be a Map or a POJO) as long as Jackson is able to encode it.
For example, you can create a Message
from the previous section and pass it to the POST
method:
Sending a JSON body
Flux<HttpResponse<Message>> call = Flux.from(client.exchange(
POST("/greet", new Message("Hello John")), (1)
Message.class (2)
));
Sending a JSON body
Flux<HttpResponse<Message>> call = Flux.from(client.exchange(
POST("/greet", new Message("Hello John")), (1)
Message (2)
))
Sending a JSON body
val call = client.exchange(
POST("/greet", Message("Hello John")), Message::class.java (2)
)
1 | An instance of Message is created and passed to the POST method |
2 | The same class decodes the response |
With the above example the following JSON is sent as the body of the request:
Resulting JSON
{"text":"Hello John"}
The JSON can be customized using Jackson Annotations.