jackson Unrecognized field
Asked Answered
I

5

42

I use jackson for converting JSON to Object class.

JSON:

{
    "aaa":"111",
    "bbb":"222", 
    "ccc":"333" 
}

Object Class:

class Test{
    public String aaa;
    public String bbb;
}

Code:

ObjectMapper mapper = new ObjectMapper();
Object obj = mapper.readValue(content, valueType);

My code throws exception like that:

org.codehaus.jackson.map.exc.UnrecognizedPropertyException: Unrecognized field "cccc" (Class com.isoftstone.banggo.net.result.GetGoodsInfoResult), not marked as ignorable

And I don't want to add a prop to class Test,I just want jackson convert the exist value whith is also exist in Test.

Invalidity answered 7/12, 2011 at 2:9 Comment(0)
C
80

Jackson provides a few different mechanisms to configure handling of "extra" JSON elements. Following is an example of configuring the ObjectMapper to not FAIL_ON_UNKNOWN_PROPERTIES.

import org.codehaus.jackson.annotate.JsonAutoDetect.Visibility;
import org.codehaus.jackson.annotate.JsonMethod;
import org.codehaus.jackson.map.DeserializationConfig;
import org.codehaus.jackson.map.ObjectMapper;

public class JacksonFoo
{
  public static void main(String[] args) throws Exception
  {
    // { "aaa":"111", "bbb":"222", "ccc":"333" }
    String jsonInput = "{ \"aaa\":\"111\",
                          \"bbb\":\"222\",
                          \"ccc\":\"333\" }";

    ObjectMapper mapper = new ObjectMapper().setVisibility(JsonMethod.FIELD,
                         Visibility.ANY);
    mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES,
                     false);

    Test test = mapper.readValue(jsonInput, Test.class);
  }
}

class Test
{
  String aaa;
  String bbb;
}

For other approaches, see http://wiki.fasterxml.com/JacksonHowToIgnoreUnknown

Catholicity answered 7/12, 2011 at 6:38 Comment(1)
Note that for 2.x you would use mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); instead.Quisling
L
41

As of Jackson 2.0 the inner enum (DeserializationConfig.Feature) has been moved to a standalone enum (DeserializationFeature):

mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

Lifeguard answered 21/11, 2012 at 16:12 Comment(0)
S
21

If you are using Jackson 2.0 (fasterxml)

ObjectMapper mapper = new ObjectMapper();
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
S answered 7/1, 2014 at 19:5 Comment(0)
C
10

It is important to beforehand notice critical change of the model that can result breakdown of business logic.

To better control over application is better to handle this exception manually.

objectMapper.addHandler(new DeserializationProblemHandler() {

            @Override
            public boolean handleUnknownProperty(DeserializationContext ctxt,
                    JsonParser jp, JsonDeserializer<?> deserializer,
                    Object beanOrClass, String propertyName)
                    throws IOException, JsonProcessingException {

                String unknownField = String.format("Ignoring unknown property %s while deserializing %s", propertyName, beanOrClass);
                Log.e(getClass().getSimpleName(), unknownField);
                return true;
            }
        });

Return true to handle UnrecognizedPropertyException

Do not ignore silently unrecognized fields.

Cleaves answered 6/12, 2016 at 18:40 Comment(2)
I think it's too broad to just say "do not". When it comes to api's and being flexible to changing versions, I'm a fan of Postel's Law. en.wikipedia.org/wiki/Robustness_principleSlipcover
Late to the party, but this is a good answer. @MattBroekhuis This answer doesn't conflict with Postel's law, and it is flexible. What it does do is give someone, somewhere, a chance to figure out why and API did not handle something as expected instead of simply obscuring useful information.Logy
S
1

As per this documentation, you can use the Jackson2ObjectMapperBuilder class to build your ObjectMapper. This Jackson2ObjectMapperBuilder is available in spring-web dependency jar.

import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder

@Autowired
Jackson2ObjectMapperBuilder objectBuilder;

ObjectMapper mapper = objectBuilder.build();
String json = "{\"id\": 1001}";

By default, Jackson2ObjectMapperBuilder disables the error unrecognizedpropertyexception.

Similitude answered 13/11, 2021 at 18:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.