This question already has an answer here:
I am writing a mixin to deserialize a string into javax.servlet.http.Cookie
Mixin.java
package a; import org.codehaus.jackson.annotate.JsonProperty; public abstract class MixIn { MixIn(@JsonProperty("name") String name, @JsonProperty("value") String value) { } }
HelloWorld.java
package b; import a.MixIn; ObjectMapper mapper = new ObjectMapper(); mapper.getDeserializationConfig().addMixInAnnotations(Cookie.class, MixIn.class); Cookie aCookie = mapper.readValue("{"name":"abc","value":"xyz"}", Cookie.class);
It seems to provide "JsonMappingException: No suitable constructor found for type [simple type, class javax.servlet.http.Cookie]" error.
Please do note that
- Mixin is (has to be) defined as a separate class (NOT an inner class, not static)
- Mixin and the class where its used are (have to be) in 2 different packages.
I am using jackson 1.9.9
Don't you need to include @JsonCreator over the mixin constructor? I'm still struggling with a similar issue myself, so I'm not 100% certain here.
My question: @JsonCreator and mixin via Module not working for 3rd Party Class
Update: my example in the above question is working, your comment on the mixin needing to be in a separate package and not an inner class did the trick, thanks!
Creating a separate class for the Mixin,
In the mapper class add this,
This will resolve the issue. Make sure that the MixinClass is a separate .java file and not an inner class.