Java 在Json响应中包含空值
我希望看到带有空字段的json响应。即使在json模式中将defalut值设置为null,也不会显示这些字段。我认为,所有空字段都会在内部进行过滤。例如Java 在Json响应中包含空值,java,json,Java,Json,我希望看到带有空字段的json响应。即使在json模式中将defalut值设置为null,也不会显示这些字段。我认为,所有空字段都会在内部进行过滤。例如 "streetAddress": { "line1": "2374 WATER WORKS RD" "line2" : null }, 目前,如果我没有设置line2,它不会出现在响应中。简单地说,我得到了 "streetAddress": { "li
"streetAddress": {
"line1": "2374 WATER WORKS RD"
"line2" : null
},
目前,如果我没有设置line2,它不会出现在响应中。简单地说,我得到了
"streetAddress": {
"line1": "2374 WATER WORKS RD"
}
pom.xml
POJO是使用maven插件生成的
<plugin>
<groupId>org.jsonschema2pojo</groupId>
<artifactId>jsonschema2pojo-maven-plugin</artifactId>
<version>0.4.11</version>
<configuration>
<sourceDirectory>${basedir}/src/main/resources/schema</sourceDirectory>
<targetPackage>com.dnb.daas.matchplus.product.ng.schema.request</targetPackage>
<includeJsr303Annotations>true</includeJsr303Annotations>
<initializeCollections>false</initializeCollections>
</configuration>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
</plugin>
POJO示例:
@JsonInclude(JsonInclude.Include.NON_NULL)
@Generated("org.jsonschema2pojo")
@JsonPropertyOrder({
"line1",
"line2"
})
public class StreetAddress {
@JsonProperty("line1")
private String line1;
@JsonProperty("line2")
private String line2;
在返回json响应之前,我尝试将一个sysout放出来,我可以看到存在空字段
[line1=2374水厂路,line2=AnglebracketOpennullAngleBracketClose]
我正在使用斯威格和波斯曼作为剩余客户。在这两种情况下,都不会出现空字段。此maven配置实现了以下功能:
<plugin>
<groupId>org.jsonschema2pojo</groupId>
<artifactId>jsonschema2pojo-maven-plugin</artifactId>
<version>0.4.37</version>
<executions>
<execution>
<id>exec-id</id>
<configuration>
<sourceDirectory>${basedir}/src/main/resources/schema/json</sourceDirectory>
<targetPackage>org.jozefowicz</targetPackage>
<inclusionLevel>ALWAYS</inclusionLevel>
</configuration>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
</plugin>
org.jsonschema2pojo
jsonschema2pojo maven插件
0.4.37
执行代码
${basedir}/src/main/resources/schema/json
org.jozefowicz
总是
生成
如果您使用的是Micronaut框架,那么下面的代码可以工作
Application.yml
jackson:
serializationInclusion: ALWAYS
您是否尝试过删除
@JsonInclude(JsonInclude.Include.NON_NULL)
?很好。。。我尝试了几件事情来删除@JsonInclude(JsonInclude.Include.NON_NULL)并将其更改为@JsonInclude(JsonInclude.Include.ALWAYS)。在这两种情况下,我都可以在rest客户机中看到null字段。非常感谢你。您能告诉我如何删除这个标记或将其修改为@JsonInclude(JsonInclude.Include.ALWAYS),因为我正在使用maven从json模式中自动生成pojo。
jackson:
serializationInclusion: ALWAYS