java - Why does Jackson ignore @JsonCreator annotation in my auto-generated POJO Enum? -
as usual, there probaly simple solution problem:
i have json schema snippet defines following enum:
"title" : { "type": "string", "enum": ["mr", "miss", "mrs", "ms"], "description": "the person's title" }
my company's framework uses jsonschema2pojo , maven create necessary pojo (title
lives in clazz
, title
part of clazz
in json schema - clazz name being made - replace employee or customer or whatever like):
generated pojo
@generated("org.jsonschema2pojo") public static enum title { mr("mr"), miss("miss"), mrs("mrs"), ms("ms"); private final string value; private static map<string, clazz.title> constants = new hashmap<string, clazz.title>(); static { (clazz.title c: values()) { constants.put(c.value, c); } } private title(string value) { this.value = value; } @jsonvalue @override public string tostring() { return this.value; } @jsoncreator public static clazz.title fromvalue(string value) { clazz.title constant = constants.get(value); if (constant == null) { throw new illegalargumentexception(value); } else { return constant; } } }
when run request containing following against it:
... "title" : "mr", ...
i error thrown @ me:
com.fasterxml.jackson.databind.exc.invalidformatexception: can not construct instance of com.example.foo.representations.jaxb.clazz$title string value 'mr': value not 1 of declared enum instance names: [mr, miss, mrs, ms] @ [source: org.apache.cxf.transport.http.abstracthttpdestination$1@1a372b7; line: 4, column: 3] (through reference chain: com.example.foo.representations.jaxb.myschema["clazz"]->com.example.foo.representations.jaxb.clazz["title"])
clearly, "mr" in enum.
when debugging, can see runs through following classes (stack):
findenum():120, enumresolver (com.fasterxml.jackson.databind.util) deserialize():79, enumdeserializer (com.fasterxml.jackson.databind.deser.std)
it looks only interested in enum's "keys" (i.e. constants, e.g. "mr
" instead of "mr
"). i'm guessing @jsoncreator
annotation ignored reason.
any idea how can fix issue? there configuration value might set anywhere might cause behaviour? (i'm working on big projects; if know need can search code base; maybe developer "misconfigured" somewhere...) or might issue title
lives in clazz
? need throw @jsonproperty
in measure? (if so, how exactly?)
we using jackson-core, -annotations, , -databind 2.4.2.
update: tried stand-alone project, following code, , worked flawlessly - means there must sort of setting prevents annotation being taken account...
objectmapper mapper = new objectmapper(); // create once, reuse clazz value = mapper.readvalue(new file("resources/data.json"), clazz.class);
so, turns out had following in our spring config:
<bean id="mapper" class="com.example.foo.jaxb.links.jsonmapper" /> <!-- ... --> <jaxrs:providers> <bean id="jsonprovider" class="com.fasterxml.jackson.jaxrs.json.jacksonjaxbjsonprovider"> <constructor-arg ref="mapper" /> <constructor-arg> <value></value> </constructor-arg> </bean> </jaxrs:providers>
jsonmapper extends com.fasterxml.jackson.databind.objectmapper
, , sets jackson settings on in constructor. 1 thing had missed in desperation, however, bit of code:
// use jaxb annotations (only) setannotationintrospector(new jaxbannotationintrospector( com.fasterxml.jackson.databind.type.typefactory.defaultinstance()));
and pretty says on tin, seems: stops jackson evaluating @jsoncreator
, @jsonvalue
annotations. once "jackson annotation suppressor" gone, good.
what i'd understand how works, if has helpful links docs/how-tos/manuals/books either spring stuff or suppression of annotations, appreciated. in meantime, fixes problem. :)
Comments
Post a Comment