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

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的完整实例,并且所有属性都存在.

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.

推荐答案

这个问题解决了

Add support for Jackson serialization views

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

这是SPR-7156.

Status: Resolved

Description

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.

Available on Spring ver >= 4.1

UPDATE

跟着这个link.用一个例子解释@JsonView注释.

Json相关问答推荐

从Razor Pages的AJAX Json呈现DataTables问题.Net GET

错误:在 NX 工作区中找不到模块../swagger.json

jq:将一个数组与多个数组连接起来

使用不同行数的数据创建嵌套Jolt

带有 API 测试的 Typescript JSON 模式验证

如何在JQ中展平多维数组

如何使用nifi从json文件中过滤属性

shell解析json并循环输出组合变量

如何为包含一些固定值并可能具有其他附加值的数组字符串创建数组 json 架构

如何使用 boost::json::value 调用无参数函数

将哈希表转换为 json 后,Powershell 缺少数组

C# 合并 2 个几乎相同的 JSON 对象

JSON对象中的JavaScript递归搜索

在 JSON 编码的 HTML5 数据属性中转义/编码单引号

如何使用 LWP 发出 JSON POST 请求?

Spring restTemplate 获取原始 json 字符串

AJAX 将 JavaScript 字符串数组发布到 JsonResult 作为 List 总是返回 Null?

JSON 模式 - 如果对象*不*包含特定属性则有效

PHP json_encode json_decode UTF-8

如何从 MVC 控制器返回 Json 对象以查看