我曾尝试使用Jersey和Jackson将java对象序列化为json并发布,但是,当java对象中的某些字段为null时,我需要发送没有该字段的json。
Java对象如下
public class ProbeData
{
private String deviceId;
@JsonInclude(Include.NON_NULL)
private Double[] location = null;
private Map<String, String> tags = new HashMap<String, String>();
public ProbeData() {}
public ProbeData(String deviceId, Double[] location, Map<String, String> tags)
{
this.deviceId = deviceId;
this.location = location;
this.tags = tags;
}
}
泽西岛邮政请求如下
baseURL = "http://localhost:9090/";
clientConfig = new ClientConfig().register(new JacksonFeature());
client = ClientBuilder.newClient(clientConfig);
HttpAuthenticationFeature auth = HttpAuthenticationFeature.basic("admin", "admin");
webTarget = client.target(baseURL).register(auth);
webTarget.path(serviceName).request(MediaType.APPLICATION_JSON_TYPE).post(Entity.entity(probeData, MediaType.APPLICATION_JSON), Response.class).getStatus();
然而,Json仍然成为
"deviceId" : "1000000002",
"location" : [
0,
0
],
"tags" : {
},
我期待的地方
"deviceId" : "1000000002",
"tags" : {
},
导入和mvn依赖关系如下
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>2.10.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.10</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.4.0</version>
</dependency>
@JsonInclude在使用Jersey webTarget请求时是否有效?
感谢任何帮助