用于将json字段绑定到POJO中具有不同名称的字段的注释

时间:2021-11-20 00:59:40

Java class (used as a Data Transfer Object):

Java类(用作数据传输对象):

class Resource also has a field named id with a different type along with its getter and setter, hence the syntax error.

class Resource还有一个名为id的字段,它带有不同的类型及其getter和setter,因此语法错误。

class A extends Resource
{
   private int id;

   public int getId() { return id; }   // syntax error as getId() function already exists in Resource
   public void setId(int id) { this.id = id; }
}

Since the above class is a DTO, a JSON response (with field id) will be mapped to it, and getId() cannot be used, I want to change the field to _id_ and change getter and setter correspondingly, and mark it with an annotation saying bind this to id field.

由于上面的类是DTO,JSON响应(带字段id)将被映射到它,并且getId()不能被使用,我想将字段更改为_id_并相应地更改getter和setter,并用一个标记它注释说明将此绑定到id字段。

Note: I'm using spring boot. I tried using @JsonProperty annotation but that didn't work. Is there an annotation for doing this in spring?

注意:我正在使用弹簧靴。我尝试使用@JsonProperty注释,但这不起作用。春天这样做是否有注释?

2 个解决方案

#1


1  

public A extends Resource {
    private int id;

    @JsonProperty("_id")
    public int getId() {
      return id;
    }

    @JsonProperty("id")
    public void setId(int id) {
      this.id = id;
    }
}

the method names should be different, so jackson parses it as different fields, not as one field.

方法名称应该不同,因此jackson将其解析为不同的字段,而不是一个字段。

#2


1  

Googled and found this question: Jackson serialization: how to ignore superclass properties. Adapted it for your problem.

谷歌搜索并发现这个问题:杰克逊序列化:如何忽略超类属性。适应您的问题。

public class A extends B {
    private int id;

    public A(int id) {
        super.setId("id" + id);
        this.id = id;
    }

    @Override
    @JsonProperty("_id_")
    public String getId() {
        return super.getId();
    }

    @Override
    @JsonProperty("_id_")
    public void setId(String id) {
        super.setId(id);
    }

    @JsonProperty("id")
    public int getIntId() {
        return id;
    }

    @JsonProperty("id")
    public void setIntId(int id) {
        this.id = id;
    }
}

public class B {
    private String id;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }
}

Tested it with this:

用这个测试:

@RestController
public class TestController {
    @GetMapping("/test")
    public A test() {
        return new A(1);
    }
}

And the output was:

输出是:

{
  "_id_": "id1",
  "id": 1
}

#1


1  

public A extends Resource {
    private int id;

    @JsonProperty("_id")
    public int getId() {
      return id;
    }

    @JsonProperty("id")
    public void setId(int id) {
      this.id = id;
    }
}

the method names should be different, so jackson parses it as different fields, not as one field.

方法名称应该不同,因此jackson将其解析为不同的字段,而不是一个字段。

#2


1  

Googled and found this question: Jackson serialization: how to ignore superclass properties. Adapted it for your problem.

谷歌搜索并发现这个问题:杰克逊序列化:如何忽略超类属性。适应您的问题。

public class A extends B {
    private int id;

    public A(int id) {
        super.setId("id" + id);
        this.id = id;
    }

    @Override
    @JsonProperty("_id_")
    public String getId() {
        return super.getId();
    }

    @Override
    @JsonProperty("_id_")
    public void setId(String id) {
        super.setId(id);
    }

    @JsonProperty("id")
    public int getIntId() {
        return id;
    }

    @JsonProperty("id")
    public void setIntId(int id) {
        this.id = id;
    }
}

public class B {
    private String id;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }
}

Tested it with this:

用这个测试:

@RestController
public class TestController {
    @GetMapping("/test")
    public A test() {
        return new A(1);
    }
}

And the output was:

输出是:

{
  "_id_": "id1",
  "id": 1
}