在 Spring MVC 中使用 @JsonView [英] Using @JsonView with Spring MVC

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

问题描述

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

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" />

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

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

推荐答案

@JsonView is 已经在 J​​ackson JSON 处理器中支持.

为 Jackson 1.9.12 更新

New Updated for Jackson 1.9.12

根据 v1.8.4 文档 我使用的函数 writeValueUsingView 现在不推荐使用strong> 改用 ObjectMapper.viewWriter(java.lang.Class)…然而,这也被弃用 从 1.9 开始,改用 writerWithView(Class)!(见 v1.9.9 文档)

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)

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

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";
    }
}

以前的您需要实例化 ObjectMapper 并使用自定义视图写出对象,如图 此处,或在此示例中:

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

定义视图:

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 更适合您的需求.

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

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

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