File tree Expand file tree Collapse file tree 1 file changed +21
-0
lines changed Expand file tree Collapse file tree 1 file changed +21
-0
lines changed Original file line number Diff line number Diff line change @@ -2840,6 +2840,27 @@ which allow rendering only a subset of all fields in an `Object`. To use it with
28402840NOTE: `@JsonView` allows an array of view classes, but you can specify only one per
28412841controller method. If you need to activate multiple views, you can use a composite interface.
28422842
2843+ If you want to do the above programmatically, instead of declaring an `@JsonView` annotation,
2844+ wrap the return value with `MappingJacksonValue` and use it to supply the serialization view:
2845+
2846+ ====
2847+ [source,java,indent=0]
2848+ [subs="verbatim,quotes"]
2849+ ----
2850+ @RestController
2851+ public class UserController {
2852+
2853+ @GetMapping("/user")
2854+ public MappingJacksonValue getUser() {
2855+ User user = new User("eric", "7!jd#h23");
2856+ MappingJacksonValue value = new MappingJacksonValue(user);
2857+ value.setSerializationView(User.WithoutPasswordView.class);
2858+ return value;
2859+ }
2860+ }
2861+ ----
2862+ ====
2863+
28432864For controllers that rely on view resolution, you can add the serialization view class
28442865to the model, as the following example shows:
28452866
You can’t perform that action at this time.
0 commit comments