Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java @JsonIgnore不会忽略hibernate实体中的字段_Java_Json_Hibernate_Jackson - Fatal编程技术网

Java @JsonIgnore不会忽略hibernate实体中的字段

Java @JsonIgnore不会忽略hibernate实体中的字段,java,json,hibernate,jackson,Java,Json,Hibernate,Jackson,我有一个具有Id属性的实体“Task”,但我不需要在JSON文件中返回该字段 @Entity public class Task { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @JsonIgnore private Integer Id; @JsonProperty("task") private String taskName; private String stat

我有一个具有Id属性的实体“Task”,但我不需要在JSON文件中返回该字段

@Entity
public class Task {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @JsonIgnore
    private Integer Id;
    @JsonProperty("task")
    private String taskName;

    private String status;
    //getter and setter
}
但是,当我发出get请求时,注释@JsonIgnore不会过滤该字段,请参见以下内容:

{
    "status": "started",
    "timestamps": {
        "submitted": "2018-12-31T00:34:20.718+0000",
        "started": "2018-12-31T00:34:20.718+0000",
        "completed": "2018-12-31T00:34:20.718+0000"
    },
    "id": 40001,
    "task": "q094hiu3o"
}

防止显示“Id”的正确方法是什么

您可以尝试仅在getter上添加
@JsonIgnore

    @JsonIgnore
    public Integer getId() {
        return id;
    }
此外,我建议在您的id字段中添加
@JsonProperty
注释,如果您正在使用的Jackson版本中有此注释:

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@JsonProperty(access = Access.WRITE_ONLY)
private Integer id;
只写 访问设置,这意味着只能为反序列化写入(设置)属性,但在序列化时不会读取(获取),也就是说,序列化中不包含属性值


因此,这里是jackson在
hibernate
中遇到的问题,请尝试在类级别上使用
@jsonignoreporties

@JsonIgnoreProperties(ignoreUnknown = true, 
                  value = {"id"})

试着在getter上添加它method@Deadpool它已经有了所有的Getter和setter,我的意思是在Getter方法上添加注释,然后尝试我观察到的另一件事,case difference
private Integer Id
“id”:40001,
很高兴知道这些方法可能存在错误,我通常使用
@JsonIgnoreProperties
来忽略未知问题,因为您将序列化与反序列化混淆了。