mercredi 1 avril 2015

Polimorphic Jackson and Spring Container

I've got some method, which has @RequestBody parameter as abstract class(jackson create concrete class dependes on one of json parameters).


This class looks some like that:



@JsonTypeInfo(use = Id.NAME, include = As.PROPERTY, property = JsonParams.PolimorphicParam)
@JsonSubTypes({ @JsonSubTypes.Type(A.class), @JsonSubTypes.Type(B.class) })
public abstract class X{
protected String someProperty;
}


In Jackson it's really cool future, because i've some kind of class hierarchy with inherited properties, but i want more. I want every child class to overrite method, which produce some object. To produce this object i need inject Service bean into this class, but jackson create it with new operator out of spring container.


Is it any way to @Autowired this service? I know there is ApplicationContextProvider which can help me to find bean in context:



EngineService engine= (EngineService) ApplicationContextProvider.getApplicationContext().getBean(
"engine");


But it looks awful and I want to autowired it.


And another question, is it good to give some behavior jackson deserialized objects?


Aucun commentaire:

Enregistrer un commentaire