What is the purpose of elementClass parameter in ServerResponse.BodyBuilder?

Viewed 97

There's a simple model class, which is supposed to be used for response body serialization:

public class MyModel{
  public String name;
}

Using it in an arbitrary Spring 5 handler function:

public Mono<ServerResponse> getSometing(ServerRequest request) {
  MyModel model = new MyModel();
  model.name = "abc";

  return ServerResponse.ok().body(Mono.just(model), %anyClass%);
}

I tried to substitute for %anyClass% different classes: Object, String, MyModel, RuntimeException - and the result is always the same. Every time the model instance had been serialized to a json {"name" : "abc"}.

It also works for more complex models with nested objects and arrays.

Could you comment, what is the purpose of the second parameter (elementClass ) if it does not affect the result? Why cannot we use Object.class always?

1 Answers
Related