Jackson 的 @JsonView、@JsonFilter 和 Spring [英] Jackson's @JsonView, @JsonFilter and Spring

查看:53
本文介绍了Jackson 的 @JsonView、@JsonFilter 和 Spring的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以使用 Jackson @JsonView@JsonFilter 批注修改 Spring MVC 控制器返回的 JSON,同时使用 MappingJacksonHttpMessageConverter 和Spring 的 @ResponseBody@RequestBody 注释?

Can one use the Jackson @JsonView and @JsonFilter annotations to modify the JSON returned by a Spring MVC controller, whilst using MappingJacksonHttpMessageConverterand Spring's @ResponseBody and @RequestBody annotations?

public class Product
{
    private Integer id;
    private Set<ProductDescription> descriptions;
    private BigDecimal price;
    ...
}


public class ProductDescription
{
    private Integer id;
    private Language language;
    private String name;
    private String summary;
    private String lifeStory;
    ...
}

当客户端请求一组 Products 时,我想返回每个 ProductDescription 的最小版本,也许只是它的 ID.然后在随后的调用中,客户端可以使用此 ID 来请求具有所有属性的 ProductDescription 的完整实例.

When the client requests a collection of Products, I'd like to return a minimal version of each ProductDescription, perhaps just its ID. Then in a subsequent call the client can use this ID to ask for a full instance of ProductDescription with all properties present.

最好能够在 Spring MVC 控制器方法上指定这一点,因为调用的方法定义了客户端请求数据的上下文.

It would be ideal to be able to specify this on the Spring MVC controller methods, as the method invoked defines the context in which client was requesting the data.

推荐答案

这个问题解决了!
按照this

添加对 Jackson 序列化视图的支持

Spring MVC 现在支持 Jackon 的序列化视图进行渲染来自不同控制器的相同 POJO 的不同子集方法(例如详细页面与摘要视图).问题:SPR-7156

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

这是 SPR-7156.

状态:已解决

说明

Jackson 的 JSONView 注释允许开发人员控制方法的哪些方面被序列化.对于当前的实现,必须使用 Jackson 视图编写器,但内容类型不可用.如果作为 RequestBody 注解的一部分,可以指定一个 JSONView 会更好.

Jackson's JSONView annotation allows the developer to control which aspects of a method are serialiazed. With the current implementation, the Jackson view writer must be used but then the content type is not available. It would be better if as part of the RequestBody annotation, a JSONView could be specified.

可用于 Spring 版本 >= 4.1

按照此链接.举例说明@JsonView注解.

Follow this link. Explains with an example the @JsonView annotation.

这篇关于Jackson 的 @JsonView、@JsonFilter 和 Spring的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆