这是我的对象
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>
问题
如何摆脱null
中的JSON
值?
答案 0 :(得分:1)
由于我使用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
,我的代码更清晰