如何在无法访问源代码的情况下解决Jackson中属性的冲突getter定义。

huangapple go评论190阅读模式
英文:

How to solve conflicting getter definitions for property in jackson without access to source

问题

我遇到了这个错误:

  1. HTTP状态500 - 无法编写JSON:属性"oid"存在冲突的getter定义

问题在于该类有两个类似的方法:

getOID(已弃用)和 getOid

但我无法修改这个类,因为它只是一个依赖项。

有办法解决这个问题吗?

英文:

I'm getting this error:

  1. HTTP Status 500 - Could not write JSON: Conflicting getter definitions for property "oid"

The problem is that the class has two similar methods:

getOID (deprecated) and getOid

But I cannot modify the class as it's just a dependency.

Is there a way to fix this issue?

答案1

得分: 2

如果您无法修改POJO,您可以实现自定义序列化器或使用MixIn特性

假设您的类如下所示:

  1. class Id {
  2. private int oid;
  3. @Deprecated
  4. public int getOID() {
  5. return oid;
  6. }
  7. public int getOid() {
  8. return oid;
  9. }
  10. public void setOid(int oid) {
  11. this.oid = oid;
  12. }
  13. @Override
  14. public String toString() {
  15. return "oid=" + oid;
  16. }
  17. }

您需要创建一个带有额外配置的接口:

  1. interface IdIgnoreConflictMixIn {
  2. @JsonIgnore
  3. int getOID();
  4. @JsonProperty
  5. int getOid();
  6. }

现在,您需要注册这个接口:

  1. import com.fasterxml.jackson.annotation.JsonIgnore;
  2. import com.fasterxml.jackson.annotation.JsonProperty;
  3. import com.fasterxml.jackson.databind.ObjectMapper;
  4. import java.io.IOException;
  5. public class JsonMixInApp {
  6. public static void main(String[] args) throws IOException {
  7. Id id = new Id();
  8. id.setOid(1);
  9. ObjectMapper mapper = new ObjectMapper();
  10. mapper.addMixIn(Id.class, IdIgnoreConflictMixIn.class);
  11. mapper.writeValue(System.out, id);
  12. }
  13. }

以上代码输出:

  1. {"oid":1}

另请参阅:

英文:

If you can not modify POJO you can implement custom serialiser or use MixIn feature.

Assume, your class looks like below:

  1. class Id {
  2. private int oid;
  3. @Deprecated
  4. public int getOID() {
  5. return oid;
  6. }
  7. public int getOid() {
  8. return oid;
  9. }
  10. public void setOid(int oid) {
  11. this.oid = oid;
  12. }
  13. @Override
  14. public String toString() {
  15. return "oid=" + oid;
  16. }
  17. }

You need to create an interface with extra configuration:

  1. interface IdIgnoreConflictMixIn {
  2. @JsonIgnore
  3. int getOID();
  4. @JsonProperty
  5. int getOid();
  6. }

Now, you need to register this interface:

  1. import com.fasterxml.jackson.annotation.JsonIgnore;
  2. import com.fasterxml.jackson.annotation.JsonProperty;
  3. import com.fasterxml.jackson.databind.ObjectMapper;
  4. import java.io.IOException;
  5. public class JsonMixInApp {
  6. public static void main(String[] args) throws IOException {
  7. Id id = new Id();
  8. id.setOid(1);
  9. ObjectMapper mapper = new ObjectMapper();
  10. mapper.addMixIn(Id.class, IdIgnoreConflictMixIn.class);
  11. mapper.writeValue(System.out, id);
  12. }
  13. }

Above code prints:

  1. {"oid":1}

See also:

huangapple
  • 本文由 发表于 2020年8月19日 03:14:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/63475238.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定