我不能用@JsonView执行这个简单的例子。我做错了什么?
$ {jackson-2-version} = 2.6.5
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson-2-version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>${jackson-2-version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson-2-version}</version>
</dependency>
完整的测试课程。
package staticTest;
import com.fasterxml.jackson.annotation.JsonView;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test;
/**
* Created by Daniel on 01/04/2016.
*/
public class Jackson2Tests {
@Test
public void JsonViewTest(){
try {
System.out.println(getMapper().writeValueAsString(new DemoClass()));
} catch (JsonProcessingException e) {
e.printStackTrace();
}
}
private ObjectMapper getMapper(){
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.writerWithView(ToShowIn.App.class);
objectMapper.readerWithView(ToShowIn.App.class);
return objectMapper;
}
public class ToShowIn {
public class App{}
public class Manager{}
}
class DemoClass{
@JsonView(ToShowIn.App.class)
private String propertyOne = "one";
private int propertyTwo = 2;
private boolean propertyThree = true;
private DemoChild propertyFour = new DemoChild();
public String getPropertyOne() {
return propertyOne;
}
public void setPropertyOne(String propertyOne) {
this.propertyOne = propertyOne;
}
public int getPropertyTwo() {
return propertyTwo;
}
public void setPropertyTwo(int propertyTwo) {
this.propertyTwo = propertyTwo;
}
public boolean isPropertyThree() {
return propertyThree;
}
public void setPropertyThree(boolean propertyThree) {
this.propertyThree = propertyThree;
}
public DemoChild getPropertyFour() {
return propertyFour;
}
public void setPropertyFour(DemoChild propertyFour) {
this.propertyFour = propertyFour;
}
class DemoChild{
private String childOne = "1";
private int childTwo = 2;
private boolean childThree = true;
public String getChildOne() {
return childOne;
}
public void setChildOne(String childOne) {
this.childOne = childOne;
}
public int getChildTwo() {
return childTwo;
}
public void setChildTwo(int childTwo) {
this.childTwo = childTwo;
}
public boolean isChildThree() {
return childThree;
}
public void setChildThree(boolean childThree) {
this.childThree = childThree;
}
}
}
}
结果:
{ “propertyOne”: “一个”, “propertyTwo”:2 “propertyThree”:真 “propertyFour”:{ “childOne”: “1”, “childTwo”:2 “childThree”:真}}
解
首先,正如@varren在下面所说,.writerWithView()
不是一个制定者。
使ObjectMapper中的一个视图持久化.setConfig()
。
objectMapper.setConfig(objectMapper.getSerializationConfig()withView(ToShowIn.App.class));
最后,最重要的是,objectMapper.disable(MapperFeature.DEFAULT_VIEW_INCLUSION);
使得JsonView的工作就像@varren所提供的jackson文档一样。
答案 0 :(得分:3)
writerWithView
和readerWithView
不是设置者,它们是ObjectWriter
和ObjectReader
构建器方法,因此您必须使用:
getMapper().writerWithView(ToShowIn.App.class).writeValueAsString(new DemoClass()))
您还必须为objectMapper禁用MapperFeature.DEFAULT_VIEW_INCLUSION
。
private ObjectMapper getMapper(){
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.disable(MapperFeature.DEFAULT_VIEW_INCLUSION);
return objectMapper;
}
以下是文档中的信息:http://wiki.fasterxml.com/JacksonJsonViews
处理“无视图”属性
默认情况下,所有没有显式视图定义的属性都是 包含在序列化中。但从杰克逊1.5开始你可以 通过以下方式更改此默认值:
objectMapper.configure(SerializationConfig.Feature.DEFAULT_VIEW_INCLUSION, 假);其中false表示不包含此类属性 启用视图。此属性的默认值为“true”。