简体   繁体   English

使用Jackson 2.9.6的运动衫中的属性名称定义冲突

[英]Conflicting property name definitions in jersey ,using Jackson 2.9.6

I have a REST service with the following modal: 我有以下模式的REST服务:

/**
 * Agent
 */

public class Agent {
  @JsonProperty("name")
  private String name = null;

  @JsonProperty("type")
  private String type = null;

  @JsonProperty("description")
  private String description = null;

  @JsonProperty("status")
  private String status = null;

  @JsonProperty("meta")
  private Object meta = null;

  @JsonProperty("Operations")
  private List<OperationsListInner> operations = null;

  @JsonProperty("Properties")
  private List<Object> properties = null;



  public Agent name(String name) {
    this.name = name;
    return this;
  }

  /**
   * Get name
   * @return name
   **/
  @JsonProperty("name")

  @ApiModelProperty(required = true, value = "")
  @NotNull
  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public Agent type(String type) {
    this.type = type;
    return this;
  }

  /**
   * Get type
   * @return type
   **/
  @JsonProperty("type")
  @ApiModelProperty(value = "")
  public String getType() {
    return type;
  }

  public void setType(String type) {
    this.type = type;
  }

  public Agent description(String description) {
    this.description = description;
    return this;
  }

  /**
   * Get description
   * @return description
   **/
  @JsonProperty("description")
  @ApiModelProperty(value = "")
  public String getDescription() {
    return description;
  }

  public void setDescription(String description) {
    this.description = description;
  }

  public Agent status(String status) {
    this.status = status;
    return this;
  }

  /**
   * Get status
   * @return status
   **/
  @JsonProperty("status")
  @ApiModelProperty(value = "")
  public String getStatus() {
    return status;
  }

  public void setStatus(String status) {
    this.status = status;
  }

  public Agent meta(Object meta) {
    this.meta = meta;
    return this;
  }

  /**
   * Get meta
   * @return meta
   **/
  @JsonProperty("meta")
  @ApiModelProperty(value = "")
  public Object getMeta() {
    return meta;
  }

  public void setMeta(Object meta) {
    this.meta = meta;
  }

  public Agent operations(List<OperationsListInner> operations) {
    this.operations = operations;
    return this;
  }

  public Agent addOperationsItem(OperationsListInner operationsItem) {
    if (this.operations == null) {
      this.operations = new ArrayList<>();
    }
    this.operations.add(operationsItem);
    return this;
  }

  /**
   * Get operations
   * @return operations
   **/
  @JsonProperty("fetchOperations")
  @ApiModelProperty(value = "")
  public List<OperationsListInner> getOperations() {
    return operations;
  }

  public void setOperations(List<OperationsListInner> operations) {
    this.operations = operations;
  }

  public Agent properties(List<Object> properties) {
    this.properties = properties;
    return this;
  }

  public Agent addPropertiesItem(Object propertiesItem) {
    if (this.properties == null) {
      this.properties = new ArrayList<>();
    }
    this.properties.add(propertiesItem);
    return this;
  }

  /**
   * Get properties
   * @return properties
   **/
  @JsonProperty("Properties")
  @ApiModelProperty(value = "")
  public List<Object> getProperties() {
    return properties;
  }

  public void setProperties(List<Object> properties) {
    this.properties = properties;
  }


  @Override
  public boolean equals(Object o) {
    if (this == o) {
      return true;
    }
    if (o == null || getClass() != o.getClass()) {
      return false;
    }
    Agent agent = (Agent) o;
    return Objects.equals(this.name, agent.name) &&
        Objects.equals(this.type, agent.type) &&
        Objects.equals(this.description, agent.description) &&
        Objects.equals(this.status, agent.status) &&
        Objects.equals(this.meta, agent.meta) &&
        Objects.equals(this.operations, agent.operations) &&
        Objects.equals(this.properties, agent.properties);
  }

  @Override
  public int hashCode() {
    return Objects.hash(name, type, description, status, meta, operations, properties);
  }


  @Override
  public String toString() {
    StringBuilder sb = new StringBuilder();
    sb.append("class Agent {\n");

    sb.append("    name: ").append(toIndentedString(name)).append("\n");
    sb.append("    type: ").append(toIndentedString(type)).append("\n");
    sb.append("    description: ").append(toIndentedString(description)).append("\n");
    sb.append("    status: ").append(toIndentedString(status)).append("\n");
    sb.append("    meta: ").append(toIndentedString(meta)).append("\n");
    sb.append("    operations: ").append(toIndentedString(operations)).append("\n");
    sb.append("    properties: ").append(toIndentedString(properties)).append("\n");
    sb.append("}");
    return sb.toString();
  }

  /**
   * Convert the given object to string with each line indented by 4 spaces
   * (except the first line).
   */
  private String toIndentedString(Object o) {
    if (o == null) {
      return "null";
    }
    return o.toString().replace("\n", "\n    ");
  }
}

now when i am trying to get this modal into my REST client, I am getting a conflicting property name error. 现在,当我尝试将此模式放入REST客户端时,出现冲突的属性名称错误。 This is happening in my GET http request. 这是在我的GET http请求中发生的。

Exception in thread "main" java.lang.IllegalStateException: Conflicting/ambiguous property name definitions (implicit name 'operations'): found multiple explicit names: [Operations, fetchOperations], but also implicit accessor: [method com.agentsDB.api.model.individualAgents.Agent#setOperations(1 params)][visible=true,ignore=false,explicitName=false]
    at com.fasterxml.jackson.databind.introspect.POJOPropertyBuilder._explode(POJOPropertyBuilder.java:1062)
    at com.fasterxml.jackson.databind.introspect.POJOPropertyBuilder.explode(POJOPropertyBuilder.java:1043)
    at com.fasterxml.jackson.databind.introspect.POJOPropertiesCollector._renameProperties(POJOPropertiesCollector.java:798)
    at com.fasterxml.jackson.databind.introspect.POJOPropertiesCollector.collectAll(POJOPropertiesCollector.java:324)
    at com.fasterxml.jackson.databind.introspect.POJOPropertiesCollector.getPropertyMap(POJOPropertiesCollector.java:287)
    at com.fasterxml.jackson.databind.introspect.POJOPropertiesCollector.getProperties(POJOPropertiesCollector.java:170)
    at com.fasterxml.jackson.databind.introspect.BasicBeanDescription._properties(BasicBeanDescription.java:164)
    at com.fasterxml.jackson.databind.introspect.BasicBeanDescription.findProperties(BasicBeanDescription.java:239)
    at com.fasterxml.jackson.databind.deser.BasicDeserializerFactory._findCreatorsFromProperties(BasicDeserializerFactory.java:346)
    at com.fasterxml.jackson.databind.deser.BasicDeserializerFactory._constructDefaultValueInstantiator(BasicDeserializerFactory.java:330)
    at com.fasterxml.jackson.databind.deser.BasicDeserializerFactory.findValueInstantiator(BasicDeserializerFactory.java:255)
    at com.fasterxml.jackson.databind.deser.BeanDeserializerFactory.buildBeanDeserializer(BeanDeserializerFactory.java:214)
    at com.fasterxml.jackson.databind.deser.BeanDeserializerFactory.createBeanDeserializer(BeanDeserializerFactory.java:137)
    at com.fasterxml.jackson.databind.deser.DeserializerCache._createDeserializer2(DeserializerCache.java:411)
    at com.fasterxml.jackson.databind.deser.DeserializerCache._createDeserializer(DeserializerCache.java:349)
    at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCache2(DeserializerCache.java:264)
    at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCacheValueDeserializer(DeserializerCache.java:244)
    at com.fasterxml.jackson.databind.deser.DeserializerCache.findValueDeserializer(DeserializerCache.java:142)
    at com.fasterxml.jackson.databind.DeserializationContext.findRootValueDeserializer(DeserializationContext.java:477)
    at com.fasterxml.jackson.databind.ObjectMapper._findRootDeserializer(ObjectMapper.java:4178)
    at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3997)
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2992)
    at com.agentsDB.api.JsonUtil.convertJSONtoJAVA(JsonUtil.java:34)
    at com.agentsDB.api.apiClientInvoke.invokeAPI(apiClientInvoke.java:54)
    at com.agentsDB.api.main.main(main.java:25)

I already read a lot of post in this regard but couldn't find any solution to this. 我已经阅读了很多有关这方面的文章,但是找不到任何解决方案。 I an using JACKSOn 2.9.6 and my JsonUtil is as follow which i have used for serialization and deserialization . 我使用的是JACKSOn 2.9.6,我的JsonUtil如下,用于序列化和反序列化。

public class JsonUtil {

private static ObjectMapper mapper;
static {
    mapper = new ObjectMapper();
}

public  String convertJAVAtoJSON(Object object){
    String JSONresult = "";
    try{
        JSONresult = mapper.writeValueAsString(object);
    } catch (JsonProcessingException e){
        System.out.println("exception occured");
        e.printStackTrace();
    }
    return JSONresult;

}


public  <T> T convertJSONtoJAVA(String jsonString, Class<T> returnTypeClass){
    T javaResult = null;
    try {
      javaResult = mapper.readValue(jsonString,returnTypeClass);

    }catch(IOException e){
       e.printStackTrace();
    }

   return javaResult;
}

can anyone help me resolve it? 有人可以帮我解决吗? I am using jersey+Jackson. 我正在使用球衣+杰克逊。

您应该将@JsonProperty("fetchOperations")更改为@JsonProperty("Operations")

Well I found the solution to this answer. 好吧,我找到了这个答案的解决方案。 FINALLY!! 最后!! Firstly, I had to remove all the @JsonProperty from all my methods(getters and setters). 首先,我必须从我的所有方法(getter和setter)中删除所有@JsonProperty Now only the fields in my class are annotated by @JsonProperty . 现在,只有班级中的字段由@JsonProperty注释。 And then in the Objectmapper object, had to add the configure line. 然后在Objectmapper对象中,必须添加配置行。

ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Lombok 和 jackson - 冲突/不明确的属性名称定义 - Lombok and jackson - Conflicting/ambiguous property name definitions 如何在不访问源代码的情况下解决 jackson 中属性的冲突 getter 定义 - How to solve conflicting getter definitions for property in jackson without access to source org.codehaus.jackson.map.JsonMappingException:属性“ matchColumn”的设置器定义冲突:com.sun.rowset.JdbcRowSetImpl - org.codehaus.jackson.map.JsonMappingException: Conflicting setter definitions for property “matchColumn”: com.sun.rowset.JdbcRowSetImpl 与 iText7 一起使用时属性的 setter 定义冲突 - Conflicting setter definitions for property when using with iText7 属性“ thenComparing”的setter定义冲突: - Conflicting setter definitions for property “thenComparing”: "名称的持久性单元定义冲突" - Conflicting persistence unit definitions for name 使用Jackson ObjectMapper和Jersey - Using Jackson ObjectMapper with Jersey 用Jackson和Jersey进行条件属性序列化 - Conditional property serialization with Jackson and Jersey 如何避免具有相同属性名称和不同数据类型的属性的 getter 定义冲突 - How to avoid the Conflicting getter definitions for property with same attribute name with different datatype 无法读取 JSON:属性的设置器定义冲突 - Could not read JSON: Conflicting setter definitions for property
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM