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

  1. Flux<HttpResponse<Message>> call = Flux.from(client.exchange(
  2. POST("/greet", new Message("Hello John")), (1)
  3. Message.class (2)
  4. ));

Sending a JSON body

  1. Flux<HttpResponse<Message>> call = Flux.from(client.exchange(
  2. POST("/greet", new Message("Hello John")), (1)
  3. Message (2)
  4. ))

Sending a JSON body

  1. val call = client.exchange(
  2. POST("/greet", Message("Hello John")), Message::class.java (2)
  3. )
1An instance of Message is created and passed to the POST method
2The same class decodes the response

With the above example the following JSON is sent as the body of the request:

Resulting JSON

  1. {"text":"Hello John"}

The JSON can be customized using Jackson Annotations.