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
2840
2840
NOTE: `@JsonView` allows an array of view classes, but you can specify only one per
2841
2841
controller method. If you need to activate multiple views, you can use a composite interface.
2842
2842
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
+
2843
2864
For controllers that rely on view resolution, you can add the serialization view class
2844
2865
to the model, as the following example shows:
2845
2866
You can’t perform that action at this time.
0 commit comments