Skip to content

Commit

Permalink
Explain how to provide serialization view programmatically
Browse files Browse the repository at this point in the history
  • Loading branch information
rstoyanchev authored and engimatic committed Sep 29, 2020
1 parent e94f600 commit 52d7c4e
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/docs/asciidoc/web/webmvc.adoc
Expand Up @@ -2638,6 +2638,25 @@ which allows rendering only a subset of all fields in an Object. To use it with
controller method. Use a composite interface if you need to activate multiple views.
====

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 relying on view resolution, simply add the serialization view class
to the model:

Expand Down

0 comments on commit 52d7c4e

Please sign in to comment.