杰克逊的@JsonView,@ JsonFilter和Spring [英] Jackson's @JsonView, @JsonFilter and Spring

查看:99
本文介绍了杰克逊的@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;
    ...
}

当客户请求<$的集合时c $ c>产品,我想返回每个 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.

推荐答案

此问题已解决!

关注这个


添加对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


状态:已解决

说明

杰克逊的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.

可用于< a href =http://docs.spring.io/spring/docs/4.1.0.BUILD-SNAPSHOT/spring-framework-reference/htmlsingle/#mvc-ann-jsonview =noreferrer> Spring ver> = 4.1

关注链接。用一个例子解释@JsonView注释。

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

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

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