简体   繁体   English

如何使用FasterXML Jackson忽略我没有的对象

[英]How to ignore object that I don't have with fasterxml jackson

I'm very new to java and android programming and in the application that I must maintain, we use fasterxml jackson to deserialize stuff from the server. 我对java和android编程非常陌生,在必须维护的应用程序中,我们使用fastxml jackson对服务器中的内容进行反序列化。

We handle must of the information (object) that we get from the server but some of the class we just don't want to handle them. 我们必须处理从服务器获得的信息(对象),但是某些类我们只是不想处理它们。

The problem is that each time the function getForObject from the RestTemplate receive one of these object that we just don't have in our code since we don't care, the library throw an exception like this: 问题在于,每一次RestTemplate中的getForObject函数接收到我们不在乎的代码(因为我们不在乎)中的其中一个对象时,库都会引发如下异常:

org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Invalid type id 'com.xxxx.sam.common.sync.SongImageMappingSyncInfo' (for id type 'Id.class'): no such class found (through reference chain: [Ljava.lang.Object[][1]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: 

I have tried to ignore this with the property "FAIL_ON_UNKNOWN_PROPERTIES" to false but it doesn't change anything since it's not a property that I want to ignore, but an object completely... 我试图通过将属性“ FAIL_ON_UNKNOWN_PROPERTIES”设置为false来忽略它,但是它并没有改变任何东西,因为它不是我想忽略的属性,而是一个完全的对象...

Anybody know a way to skip these exception? 有人知道跳过这些异常的方法吗?

Thanks! 谢谢!

I always have this annotation above my objects which I want to deserialize: 我想反序列化的对象上面总是有这个注释:

@JsonIgnoreProperties(ignoreUnknown = true)
public class ClassToBeDeserialized() {
}

There doesn't seems to be a way to do this by design. 似乎没有办法通过设计来做到这一点。 It's kind of weird to receive a json object and ignore it. 接收一个json对象并忽略它有点奇怪。 It's possible to ignore properties but to do so, your object must exist. 可以忽略属性,但是这样做,您的对象必须存在。

So what we did is to create the object in our code and we ignore it once it's deserialized. 因此,我们要做的是在代码中创建对象,并且在将其反序列化后将其忽略。

Not pretty but it work. 不漂亮,但可以。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM