gpt4 book ai didi

java - @jsonIgnore 注释不起作用

转载 作者:行者123 更新时间:2023-12-02 02:06:15 25 4
gpt4 key购买 nike

我使用@JsonIgnore属性来忽略pojo中的一些属性,但是使用Gson库解析json后,这些字段在json响应中不会被忽略,请帮忙。

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@JsonIgnoreProperties(ignoreUnknown=true)
public class RideInvite extends RideInviteOld implements Serializable
{
private static final long serialVersionUID = -3729010679180341959L;


private double newFare = -1;
@JsonIgnore
private long prefPickupDropId;
@JsonIgnore
private String pickupLandmark;
@JsonIgnore
private String dropLandmark;
}

使用以下代码进行解析

 GsonBuilder builder = new GsonBuilder();
Gson gson = builder.create();
jsonText = gson.toJson(msgObject);

解析后的响应

{"newFare":-1.0,"prefPickupDropId":2,"savePickupDropPoints":false,"pickupDropBasedOnTraffic":true,"allowFareChange":true}

这里 prefPickupDropId 和 savePickupDropPoints 被 json 忽略,但 value 属性仍然存在于 json 文本中

我不能对字段使用@Expose,因为我的项目是这样构建的,忽略不需要的字段,json忽略并且相同的pojos用于准备http响应。早些时候这工作正常,但最近我遇到了这个问题

提前致谢

最佳答案

方法1:
您应该使用而不是使用 com.fasterxml.jackson.annotation.JsonIgnore com.google.gson.annotations.Expose达到您的要求。

这是工作代码片段 -

import java.io.Serializable;

import com.google.gson.annotations.Expose;

public class RideRecord implements Serializable {
@Expose
private double newFare = -1;

private long prefPickupDropId;

private String pickupLandmark;

private String dropLandmark;

public RideRecord(double newFare, long prefPickupDropId, String pickupLandmark, String dropLandmark) {
super();
this.newFare = newFare;
this.prefPickupDropId = prefPickupDropId;
this.pickupLandmark = pickupLandmark;
this.dropLandmark = dropLandmark;
}

}

使用如下代码进行解析:

RideRecord msgObject = new RideRecord(-1.0, 2, "sfo", "powell bart station");
GsonBuilder builder = new GsonBuilder();
builder.excludeFieldsWithoutExposeAnnotation();
Gson gson = builder.create();
String jsonText = gson.toJson(msgObject);
System.out.println(jsonText);

它将给出输出:

{"newFare":-1.0}

因为只有 newFare 是公开的字段。您可以使用 @Expose 属性来满足您的要求。

方法2:

If you don't want to use @Expose then also you can achieve your requirement by just creating Gson object as below -

RideRecord msgObject = new RideRecord(-1.0, 2, "sfo", "powell bart station");
GsonBuilder builder = new GsonBuilder();
builder.addSerializationExclusionStrategy(new ExclusionStrategy() {

@Override
public boolean shouldSkipField(FieldAttributes fieldAttributes) {

return fieldAttributes.getName().equals("prefPickupDropId") || fieldAttributes.getName().equals("pickupLandmark") || fieldAttributes.getName().equals("dropLandmark");
}

@Override
public boolean shouldSkipClass(Class<?> arg0) {
// TODO Auto-generated method stub
return false;
}
});

Gson gson = builder.create();
String jsonText = gson.toJson(msgObject);
System.out.println(jsonText);

在这种情况下,您也会得到相同的输出:

{"newFare":-1.0}

关于java - @jsonIgnore 注释不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50760359/

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