gpt4 book ai didi

java - Jackson 不忽略空值

转载 作者:太空宇宙 更新时间:2023-11-04 06:25:43 25 4
gpt4 key购买 nike

这是我的对象

public class TokenTO {
private String clientId;
private String clientSecret;
private String authCode;
private String authToken;
....
}

当我收到 JSON 响应时,我看到

{"clientId":null,"clientSecret":null,"authCode":null,"authToken":"312301658396979836978200915715157679015:80bf41c2-bc97-492f-a65e-172bef9efb7c:922000000"} 

我的JacksonContextResolver看起来像

public JacksonContextResolver() {
objectMapper = new ObjectMapper()
.setSerializationInclusion(JsonSerialize.Inclusion.ALWAYS);

final AnnotationIntrospector annotationIntrospector = new JaxbAnnotationIntrospector();
objectMapper
.enable(DeserializationConfig.Feature.READ_ENUMS_USING_TO_STRING)
.setSerializationInclusion(JsonSerialize.Inclusion.NON_NULL)
.setDeserializationConfig(objectMapper.getDeserializationConfig().withAnnotationIntrospector(annotationIntrospector))
.setSerializationConfig(objectMapper.getSerializationConfig().withAnnotationIntrospector(annotationIntrospector));
}

我尝试了 How to tell Jackson to ignore a field during serialization if its value is null? 中的答案和jackson ignore empty values (or null)但它们都不起作用。

我的 maven 依赖项看起来像

 <properties>
<jaxrs-api.version>3.0.8.Final</jaxrs-api.version>
<maven-war-plugin.version>2.4</maven-war-plugin.version>
<resteasy-jaxrs.version>3.0.9.Final</resteasy-jaxrs.version>
<jackson.version>1.9.13</jackson.version>
<jaxrs-api.version>3.0.8.Final</jaxrs-api.version>
</properties>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-xc</artifactId>
<version>${jackson.version}</version>
</dependency>

问题

如何删除 JSON 中的 null 值?

最佳答案

由于我使用的是 Java EE 7,因此我尝试使用 JSON Processing .

我所做的更改是

为了测试,我添加了

    <dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.json</artifactId>
<version>1.0.4</version>
<scope>test</scope>
</dependency>

在源代码中,我创建了 JSON

public JsonObject getAuthToken(@Nonnull final TokenManager tokenManager) {
final String token = tokenManager.getAccessToken(clientId, clientSecret, authCode);
return Json.createObjectBuilder().add("authToken", token).build();
}

为了测试,我做了一些事情

final JsonObject jsonReply = getJsonObjectFromResponse(response);
assertNotNull(jsonReply.getString("authToken"));

    @Nonnull
protected JsonObject getJsonObjectFromResponse(@Nonnull final Response response) {
final JsonReader jsonReader = Json.createReader(new StringReader(response.readEntity(String.class)));
return jsonReader.readObject();
}

现在,部署后,我只能看到我创建的内容

{"authToken":"165099393037221788471723459805657963846:8b0f2514-938f-4509-964f-ee073dbab4ed:238000000"} 

所以我根本使用Jackson,而且我的代码更加简洁

关于java - Jackson 不忽略空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26822561/

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