在Json Response中包含NULL值

时间:2015-07-08 11:09:52

标签: java json

我希望看到带有空字段的json响应。即使在json架构中将defalut值设置为null,也不会显示这些字段。我认为,所有空字段都在内部进行过滤。对于前。

"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>

Json Schema:

"streetAddress": {
  "id": "streetAddress",
  "type": "object",
  "properties": {
    "line1": {
      "id": "line1",
      "type": "string"                        
    },
    "line2": {                        
      "id": "line2",
      "type": "string"
    }
  },
  "additionalProperties": false                   
}

示例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 WATER WORKS RD,line2 = AnglebracketOpen null AngleBracketClose]

我使用Swagger和Post Man作为其余客户端。在两个空字段中都没有出现。

2 个答案:

答案 0 :(得分:0)

此maven配置可以解决问题:

ggplot() +

  geom_raster(data = points.FR %>%
                mutate(dist = pmin(dist, 100)),
             aes(x = x, y = y, fill = dist)) +

  # optional. shows outline of France for reference
  geom_polygon(data = map.FR,
               aes(x = long, y = lat, group = group),
               fill = NA, colour = "white") +

  # define colour range, mid point, & transformation (if desired) for fill
  scale_fill_gradient2(low = "yellow", mid = "red", high = "black", 
                       midpoint = 4, trans = "sqrt") +

  labs(x = "longitude", 
       y = "latitude",
       fill = "Distance in km") +

  coord_quickmap()

答案 1 :(得分:0)

如果您使用的是 Micronaut 框架,那么下面的代码有效

Application.yml

jackson:
  serializationInclusion: ALWAYS