Browse Source

Explain how to provide serialization view programmatically

Closes gh-25596
pull/23967/head
Rossen Stoyanchev 5 years ago
parent
commit
5de4f756f3
  1. 21
      src/docs/asciidoc/web/webmvc.adoc

21
src/docs/asciidoc/web/webmvc.adoc

@ -2840,6 +2840,27 @@ which allow rendering only a subset of all fields in an `Object`. To use it with @@ -2840,6 +2840,27 @@ which allow rendering only a subset of all fields in an `Object`. To use it with
NOTE: `@JsonView` allows an array of view classes, but you can specify only one per
controller method. If you need to activate multiple views, you can use a composite interface.
If you want to do the above programmatically, instead of declaring an `@JsonView` annotation,
wrap the return value with `MappingJacksonValue` and use it to supply the serialization view:
====
[source,java,indent=0]
[subs="verbatim,quotes"]
----
@RestController
public class UserController {
@GetMapping("/user")
public MappingJacksonValue getUser() {
User user = new User("eric", "7!jd#h23");
MappingJacksonValue value = new MappingJacksonValue(user);
value.setSerializationView(User.WithoutPasswordView.class);
return value;
}
}
----
====
For controllers that rely on view resolution, you can add the serialization view class
to the model, as the following example shows:

Loading…
Cancel
Save