我有一個類Test,它有一個類型為 的成員變量NestedTest。import com.fasterxml.jackson.annotation.JsonView;class Test{ String s1; @JsonView(ConvertView.class) int a; @JsonView(ConvertView.class) NestedTest nestedObj;}class NestedTest{ int n; String s;}序列化類 Test 的對象后,字段nestedObj 顯示為空。我猜測這是因為我禁用了默認視圖包含,該視圖包含也應用于 NestedTest 對象的字段。如果出現這種情況,我該如何預防呢?我想禁用包含沒有附加視圖的字段,僅適用于外部類,而不適用于對象成員變量的字段。import com.fasterxml.jackson.databind.ObjectMapper;import com.fasterxml.jackson.databind.ObjectWriter;import com.fasterxml.jackson.databind.MapperFeature;..................Test test =new Test();................ObjectWriter writer = new ObjectMapper().disable(MapperFeature.DEFAULT_VIEW_INCLUSION).writer();String s= writer.withView(ConvertView.class).writeValueAsString(test);System.out.println(s);我的 pom.xml<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>Inheritance</groupId> <artifactId>Inheritance</artifactId> <version>0.0.1-SNAPSHOT</version> <build> <sourceDirectory>src</sourceDirectory> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins>
1 回答

30秒到達戰場
TA貢獻1828條經驗 獲得超6個贊
指定@JsonView的班級級別NestedTest:
@JsonView(ConvertView.class)
class NestedTest{
int n;
String s;
}
對于自定義類,您需要為樹中的每個類指定@JsonView您想要使哪些字段對視圖可見。
添加回答
舉報
0/150
提交
取消