【问题标题】:Deserialization exception to deserialize json String反序列化json字符串的反序列化异常
【发布时间】:2019-08-22 20:19:10
【问题描述】:

得到

com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: 

Unrecognized field "operationMessage" (class worker.lib.message.OperationMessage), not marked as ignorable (9 known properties: "packageMessage", "sourceAsset", "operation", "publishMessage", "requestId", "remixMessage", "targetAsset", "jobId", "intermediates"])
    
 at [Source: (String)"{"operationMessage":{"sourceAsset":{"path":"/a/b/c.txt","repoId":"testId","region":"va6"},"targetAsset":{"path":"/folder4","repoId":"id2","region":"va6"},"jobId":"7c540211d1054442940211d10594426e","intermediates":false}}"; line: 1, column: 22] (through reference chain: 
worker.lib.message.OperationMessage["operationMessage"])

我的 OperationMessage 类是

@JsonInclude(JsonInclude.Include.NON_NULL)
@Data //generates getter and setters
public class OperationMessage {
  
  @JsonProperty(value = "jobId")
  private String jobId;
  
  @JsonProperty(value = "operation")
  private Operation operation;
  
  @JsonProperty(value = "intermediates")
  private Boolean intermediates; //for copy-move 
  
  private AssetProperties sourceAsset;
  
  private AssetProperties targetAsset;
  
  private PublishMessage publishMessage; //fields related to publish Operation. 
  
  private RemixMessage remixMessage; //fields related to remix Operation.
  
  private PackageMessage packageMessage;//fields related to package Operation

  private String requestId;

  @Override
  public String toString() {
    return "OperationMessage [jobId=" + jobId + ", operation=" + operation + "]";
  }
  

}

有人可以提出这里的问题吗?

【问题讨论】:

    标签: java rest jackson jackson-databind


    【解决方案1】:

    您应该做的一些事情如下:

    1) 为类的成员变量添加 getter 和 setter

    2) 添加默认构造函数public class OperationMessage { }

    3) 实现Serializablepublic class OperationMessage implements Serializable

    4) 将@JsonIgnoreProperties(ignoreUnknown = true) 注解添加到您的 POJO

    【讨论】:

    • 所以我确实有 getter 和 setter 。我使用提供它们的 lombok 注释。
    • 其余的步骤你都完成了吗?
    • 其实默认构造函数也有。 @JsonIgnoreProperties(ignoreUnknown = true) 必须应用于类级别?另外,@JsonInclude(JsonInclude.Include.NON_NULL) 意思不一样吗?
    • 我在序列化时使用相同的对象,它忽略了 NULL 值。
    • {"operationMessage":{"jobId":"4ad9695d224740b499695d224700b474","intermediates":false,"sourceAsset":{"path":"/folder1/folder2/a.txt","re​​poId ":"urn:uuid:fe86e84b-d426-4e5a-b849-3381f06aae18","re​​gion":"va6"},"targetAsset":{"path":"/folder4","re​​poId":"urn:uuid: fe86e84b-d426-4e5a-b849-3381f06aae18","re​​gion":"va6"}}}
    【解决方案2】:

    您没有 getter 和 setter...或者将这些字段更改为公开的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-01-09
      • 2023-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-02
      • 2013-07-16
      相关资源
      最近更新 更多