Skip to content

Commit 1f3f9b1

Browse files
committed
Explain how to provide serialization view programmatically
Closes gh-25596
1 parent ca6ac8e commit 1f3f9b1

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

src/docs/asciidoc/web/webmvc.adoc

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2638,6 +2638,25 @@ which allows rendering only a subset of all fields in an Object. To use it with
26382638
controller method. Use a composite interface if you need to activate multiple views.
26392639
====
26402640

2641+
If you want to do the above programmatically, instead of declaring an `@JsonView` annotation,
2642+
wrap the return value with `MappingJacksonValue` and use it to supply the serialization view:
2643+
2644+
[source,java,indent=0]
2645+
[subs="verbatim,quotes"]
2646+
----
2647+
@RestController
2648+
public class UserController {
2649+
2650+
@GetMapping("/user")
2651+
public MappingJacksonValue getUser() {
2652+
User user = new User("eric", "7!jd#h23");
2653+
MappingJacksonValue value = new MappingJacksonValue(user);
2654+
value.setSerializationView(User.WithoutPasswordView.class);
2655+
return value;
2656+
}
2657+
}
2658+
----
2659+
26412660
For controllers relying on view resolution, simply add the serialization view class
26422661
to the model:
26432662

0 commit comments

Comments
 (0)