问题描述
我正在使用以下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 )
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,您可能会发现更适合您的需求。
If you are using Jackson >= 1.7 you might find that the @JSONFilter
better suits your needs.
这篇关于在Spring MVC中使用@JsonView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!