gpt4 book ai didi

java - @JsonView 在简单测试中不起作用

转载 作者:行者123 更新时间:2023-11-29 04:48:18 27 4
gpt4 key购买 nike

我无法使用@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":"one","propertyTwo":2,"propertyThree":true,"propertyFour":{"childOne":"1","childTwo":2,"childThree":true}}

解决方案

首先,正如@varren 在下面所说,.writerWithView() 不是 setter。

要在 ObjectMapper 中使用 .setConfig() 使一个 View 持久化。

objectMapper.setConfig(objectMapper.getSerializationConfig().withView(ToShowIn.App.class));

最后,也是最重要的,objectMapper.disable(MapperFeature.DEFAULT_VIEW_INCLUSION);让 JsonView 的工作方式就像@varren 贡献的 jackson 文档所说的那样。

最佳答案

writerWithViewreaderWithView 不是 setter,它们是 ObjectWriterObjectReader 构建器方法,所以你必须使用:

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

Handling of "view-less" properties

By default all properties without explicit view definition are included in serialization. But starting with Jackson 1.5 you can change this default by:

objectMapper.configure(SerializationConfig.Feature.DEFAULT_VIEW_INCLUSION, false); where false means that such properties are NOT included when enabling with a view. Default for this property is 'true'.

关于java - @JsonView 在简单测试中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36355577/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com