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

查看:112
本文介绍了在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 已经支持

新编辑: 更新了Jackson 1.9.12

New Updated for Jackson 1.9.12

根据v1.8.4 documentation 我正在使用的函数 writeValueUsingView 现在不推荐使用 使用ObjectMapper.viewWriter(java.lang.Class)而不是&hellip ;但是,从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天全站免登陆