将 @JsonView 与 Spring MVC 一起使用

发布于 2024-11-02 20:38:58 字数 244 浏览 0 评论 0原文

我正在使用以下 bean 定义来使我的 spring 应用程序以 JSON 进行通信,

<bean id="jacksonMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />

此消息转换器 bean 是否可以使用 @JsonView 注释?

I am using the following bean definition to make my spring app talking in JSON

<bean id="jacksonMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />

Is it possible with this message converter bean to use the @JsonView annotation?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

独守阴晴ぅ圆缺 2024-11-09 20:38:59

@JsonView 在 v1 版本的 Jackson JSON 处理器中已受支持 .4 起。

更新 Jackson 1.9.12

新编辑:根据 v1.8.4 文档 我正在使用的函数 writeValueUsingView 现已弃用 使用 ObjectMapper.viewWriter(java.lang.Class) 代替...但是这也已弃用 >从1.9开始,改用writerWithView(Class)! (参见 v1.9.9 文档

所以这里是一个更新的示例,使用 Spring 3.2.0 和 Jackson 1.9.12 进行测试,它仅返回 {id: 1} 而不是扩展的 {name: "name"} 因为它使用的是 .writerWithView(Views.Public.class)。切换到 Views.ExtendPublic.class 将导致 {"id":1,"name":"name"}

package com.demo.app;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import org.codehaus.jackson.map.annotate.JsonView;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.ObjectWriter;

import javax.servlet.http.HttpServletResponse;

import java.io.IOException;

@Controller
public class DemoController {
    private final ObjectMapper objectMapper = new ObjectMapper();

    @RequestMapping(value="/jsonOutput")
    @ResponseBody
    public String myObject(HttpServletResponse response) throws IOException {
        ObjectWriter objectWriter = objectMapper.writerWithView(Views.Public.class);
        return objectWriter.writeValueAsString(new MyObject());
    }

    public static class Views {
        static class Public {}
        static class ExtendPublic extends Public {}
    }

    public class MyObject {
        @JsonView(Views.Public.class) Integer id = 1;
        @JsonView(Views.ExtendPublic.class) String name = "name";
    }
}

上一次编辑: 您需要实例化ObjectMapper并使用自定义视图写出对象,如此处,或者在此示例中:

定义视图:

class Views {
    static class Public {}
    static class ExtendedPublic extends PublicView {}
    ...
}

public class Thing {
    @JsonView(Views.Public.class) Integer id;
    @JsonView(Views.ExtendPublic.class) String name;
}

使用视图:

private final ObjectMapper objectMapper = new ObjectMapper();

@RequestMapping(value = "/thing/{id}")
public void getThing(@PathVariable final String id, HttpServletResponse response) {
    Thing thing = new Thing();
    objectMapper.writeValueUsingView(response.getWriter(), thing, Views.ExtendPublic.class);
}

如果您使用 Jackson >= 1.7,您可能会发现 @JSONFilter 更适合您的需求。

@JsonView is already supported in the Jackson JSON Processor from v1.4 onwards.

New Edit: Updated for Jackson 1.9.12

According to the v1.8.4 documentation the function I was using writeValueUsingView is now Deprecated Use ObjectMapper.viewWriter(java.lang.Class) instead… however that has also been Deprecated Since 1.9, use writerWithView(Class) instead! (see v1.9.9 documentation)

So here is an updated example, tested with Spring 3.2.0 and Jackson 1.9.12 which simply returns {id: 1} and not the extended {name: "name"} since it is using the .writerWithView(Views.Public.class). Switching to Views.ExtendPublic.class will result in {"id":1,"name":"name"}

package com.demo.app;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import org.codehaus.jackson.map.annotate.JsonView;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.ObjectWriter;

import javax.servlet.http.HttpServletResponse;

import java.io.IOException;

@Controller
public class DemoController {
    private final ObjectMapper objectMapper = new ObjectMapper();

    @RequestMapping(value="/jsonOutput")
    @ResponseBody
    public String myObject(HttpServletResponse response) throws IOException {
        ObjectWriter objectWriter = objectMapper.writerWithView(Views.Public.class);
        return objectWriter.writeValueAsString(new MyObject());
    }

    public static class Views {
        static class Public {}
        static class ExtendPublic extends Public {}
    }

    public class MyObject {
        @JsonView(Views.Public.class) Integer id = 1;
        @JsonView(Views.ExtendPublic.class) String name = "name";
    }
}

Previous Edit: You need to instantiate the ObjectMapper and write out the object using a custom view as shown here, or in this example:

Define views:

class Views {
    static class Public {}
    static class ExtendedPublic extends PublicView {}
    ...
}

public class Thing {
    @JsonView(Views.Public.class) Integer id;
    @JsonView(Views.ExtendPublic.class) String name;
}

Use views:

private final ObjectMapper objectMapper = new ObjectMapper();

@RequestMapping(value = "/thing/{id}")
public void getThing(@PathVariable final String id, HttpServletResponse response) {
    Thing thing = new Thing();
    objectMapper.writeValueUsingView(response.getWriter(), thing, Views.ExtendPublic.class);
}

If you are using Jackson >= 1.7 you might find that the @JSONFilter better suits your needs.

烟─花易冷 2024-11-09 20:38:59

Spring 不支持 @JsonView 注解,但这个问题已解决!
遵循

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

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

这是 SPR-7156

状态:已解决

描述

Jackson 的 JSONView 注释允许开发人员控制方法的哪些方面被序列化。在当前的实现中,必须使用 Jackson 视图编写器,但内容类型不可用。如果可以在 RequestBody 注释中指定 JSONView,那就更好了。

可在Spring 版本 >= 4.1

谢谢 Spring!

@JsonView annotation was not supported on Spring but this issue is solved!
Follow this

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 is the 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

Thank you Spring!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文