Add support for Jackson serialization views
Spring MVC now supports Jackon's serialization views for rendering different subsets of the same POJO from different controller methods (e.g. detailed page vs summary view). Issue: SPR-7156
This commit is contained in:
committed by
Rossen Stoyanchev
parent
673a497923
commit
be0b69cbf1
@@ -29853,6 +29853,76 @@ Check out the
|
||||
{javadoc-baseurl}/org/springframework/web/bind/annotation/ControllerAdvice.html[`@ControllerAdvice`
|
||||
documentation] for more details.
|
||||
|
||||
[[mvc-ann-jsonview]]
|
||||
===== Jackson Serialization View Support
|
||||
|
||||
It can sometimes be useful to filter contextually the object that will be serialized to the
|
||||
HTTP response body. In order to provide such capability, Spring MVC has built-in support for
|
||||
rendering with http://wiki.fasterxml.com/JacksonJsonViews[Jackson's Serialization Views].
|
||||
|
||||
To use it with `@ResponseBody` controller method or methods return `ResponseEntity`, simply
|
||||
add the `@JsonView` annotation:
|
||||
|
||||
[source,java,indent=0]
|
||||
[subs="verbatim,quotes"]
|
||||
----
|
||||
@RestController
|
||||
public class UserController {
|
||||
|
||||
@RequestMapping(value = "/user", method = RequestMethod.GET)
|
||||
@JsonView(User.WithoutPasswordView.class)
|
||||
public User getUser() {
|
||||
return new User("eric", "7!jd#h23");
|
||||
}
|
||||
}
|
||||
|
||||
public class User {
|
||||
|
||||
public interface WithoutPasswordView {};
|
||||
public interface WithPasswordView extends WithoutPasswordView {};
|
||||
|
||||
private String username;
|
||||
private String password;
|
||||
|
||||
public User() {
|
||||
}
|
||||
|
||||
public User(String username, String password) {
|
||||
this.username = username;
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
@JsonView(WithoutPasswordView.class)
|
||||
public String getUsername() {
|
||||
return this.username;
|
||||
}
|
||||
|
||||
@JsonView(WithPasswordView.class)
|
||||
public String getPassword() {
|
||||
return this.password;
|
||||
}
|
||||
}
|
||||
----
|
||||
|
||||
For controllers relying on view resolution, simply add the serialization view class
|
||||
to the model:
|
||||
|
||||
[source,java,indent=0]
|
||||
[subs="verbatim,quotes"]
|
||||
----
|
||||
@Controller
|
||||
public class UserController extends AbstractController {
|
||||
|
||||
@RequestMapping(value = "/user", method = RequestMethod.GET)
|
||||
public String getUser(Model model) {
|
||||
model.addAttribute("user", new User("eric", "7!jd#h23"));
|
||||
model.addAttribute(JsonView.class.getName(), User.WithoutPasswordView.class);
|
||||
return "userView";
|
||||
}
|
||||
}
|
||||
|
||||
----
|
||||
|
||||
[[mvc-ann-async]]
|
||||
==== Asynchronous Request Processing
|
||||
Spring MVC 3.2 introduced Servlet 3 based asynchronous request processing. Instead of
|
||||
@@ -39812,6 +39882,20 @@ In the above example, we first prepare a request entity that contains the
|
||||
`MyRequestHeader` header. We then retrieve the response, and read the `MyResponseHeader`
|
||||
and body.
|
||||
|
||||
[[rest-template-jsonview]]
|
||||
===== Jackson JSON Views support
|
||||
|
||||
It is possible to specify a http://wiki.fasterxml.com/JacksonJsonViews[Jackson JSON View]
|
||||
to serialize only a subset of the object properties. For example:
|
||||
|
||||
[source,java,indent=0]
|
||||
[subs="verbatim,quotes"]
|
||||
----
|
||||
JacksonSerializationValue jsv = new JacksonSerializationValue(new User("eric", "7!jd#h23"),
|
||||
User.WithoutPasswordView.class);
|
||||
HttpEntity<JacksonSerializationValue> entity = new HttpEntity<JacksonSerializationValue>(jsv);
|
||||
String s = template.postForObject("http://example.com/user", entity, String.class);
|
||||
----
|
||||
|
||||
|
||||
[[rest-message-conversion]]
|
||||
|
||||
Reference in New Issue
Block a user