Skip to content

Commit 5de4f75

Browse files
committed
Explain how to provide serialization view programmatically
Closes gh-25596
1 parent 385c97d commit 5de4f75

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

src/docs/asciidoc/web/webmvc.adoc

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2840,6 +2840,27 @@ which allow rendering only a subset of all fields in an `Object`. To use it with
28402840
NOTE: `@JsonView` allows an array of view classes, but you can specify only one per
28412841
controller 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+
28432864
For controllers that rely on view resolution, you can add the serialization view class
28442865
to the model, as the following example shows:
28452866

0 commit comments

Comments
 (0)