2017-10-11 26 views
0

我有一個簡單的類:傑克遜反序列化 - 處理生成的值與沒有字段

com.fasterxml.jackson.databind:

@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id", scope = Endpoint.class) 
public class Endpoint { 

    @Id 
    @GeneratedValue(strategy = GenerationType.AUTO) 
    @JsonView({View.Endpoint.class, View.User.class}) 
    private Long id; 

    @NotNull 
    @NotEmpty(message = "Phone number is required.") 
    @JsonView({View.Endpoint.class, View.Call.class, View.User.class}) 
    private String phoneNumber; 

    @JsonView({View.Endpoint.class, View.Call.class, View.User.class}) 
    private String callerId; 

    @ManyToOne(optional = false) 
    @JsonView(View.Endpoint.class) 
    @ApiModelProperty(hidden = true) 
    private User user; 

    ... 

    @Override 
    @JsonView({View.Endpoint.class, View.Call.class, View.User.class}) 
    public String getUri() { 
     return EndpointController.BASE_PATH + "/" + getId(); 
    } 

} 

嘗試反序列化時,我得到了下面的錯誤。 exc.UnrecognizedPropertyException: 無法識別的字段 「URI」(類 com.example.server.telephony.endpoint.Endpoint),未標記爲 忽略(5個已知性質: 「ID」, 「標籤」, 「來電」, 「 phoneNumber「,」user「])

由於URI是一個生成的值,所以沒有字段用@JsonIgnore進行註釋。有關在這種情況下做什麼的建議?

+0

你可以考慮[混合插件](https://開頭stackoverflow.com/a/38889775/1426227)。 –

+0

當您使用@JsonIdentityInfo時,請嘗試使用註釋獲取者而不是字段 –

+0

要求擁有@JsonView ?! – Generic

回答

0

使用@JsonIgnoreProperties(ignoreUnknown = true)

標註您Endpoint類或與下列選項會影響這一反序列化映射所有的POJO配置ObjectMapper

objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);