You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Spring dependency injection for Jackson serializers [SPR-10768] #15394
Spring does not has support injecting capabilities for JsonSerializers. This is almost same as Validator part of Spring project. Serializers must be created by Spring and injected necessary parts to inside it.
a sample:
@Component
public class PhotoSerializer extends JsonSerializer<String> {
@Autowired
TestService testService;
@Override
public void serialize(String value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
jgen.writeString(testService.directory() + "/photo/".concat(value));
}
}
The lifecycle of JsonSerializer instances are usually outside of the control of Spring. It might be possible to write a custom com.fasterxml.jackson.databind.ser.SerializerFactory to do what you need.
How are you usually referring to your serializers? Are you using the @JsonSerialize annotation?
Unfortunately, the Jackson SerializerFactory mechanism is quite convoluted and not really a straightforward SPI for our purposes here, in contrast to Bean Validation's ConstraintValidatorFactory.
All, if you have custom SerializerFactory implementations along those lines that work for you, please contribute them. If general enough variants are possible here, we'll consider them for inclusion in Spring proper.
I'm also hitting this issue as I want to use a @JsonDeserialize(using = ...) on one of my domain classes.
Note as it is a specific case I want to use this annotation instead of configuring a general deserializer in the object mapper.
I think it would be good to add this capability to Spring. @Jurgen I don't get the comment that it's not really straightforward.
The abstract HandlerInstantiator class drags in quite a few recent Jackson SPI types (at compilation time and at runtime), including Converter which was introduced in Jackson 2.1... But fortunately, as of #15887, we require Jackson 2.1+ at runtime anyway.
So let's give this a try, even applying it by default if it works out... As long as it can be overridden through a user-provided HandlerInstantiator, there's nothing wrong with checking for Spring injection demand by default, just like we do for Bean Validation constraint validators.
Activity
spring-projects-issues commentedon Jul 22, 2013
Phil Webb commented
The lifecycle of
JsonSerializerinstances are usually outside of the control of Spring. It might be possible to write a customcom.fasterxml.jackson.databind.ser.SerializerFactoryto do what you need.How are you usually referring to your serializers? Are you using the
@JsonSerializeannotation?spring-projects-issues commentedon Jul 22, 2013
Cemo Koc commented
Exactly.
This is quite similar to javax.validation.Validation integration.
spring-projects-issues commentedon Apr 10, 2014
Juergen Hoeller commented
Unfortunately, the Jackson SerializerFactory mechanism is quite convoluted and not really a straightforward SPI for our purposes here, in contrast to Bean Validation's ConstraintValidatorFactory.
All, if you have custom SerializerFactory implementations along those lines that work for you, please contribute them. If general enough variants are possible here, we'll consider them for inclusion in Spring proper.
Juergen
spring-projects-issues commentedon Nov 23, 2014
Marcel Overdijk commented
I'm also hitting this issue as I want to use a
@JsonDeserialize(using = ...)on one of my domain classes.Note as it is a specific case I want to use this annotation instead of configuring a general deserializer in the object mapper.
I think it would be good to add this capability to Spring.
@JurgenI don't get the comment that it's not really straightforward.I think the approach to take here is to implement a Spring context aware
HandlerInstantiatoras this seems exactly how to this from Jackson documentation: http://wiki.fasterxml.com/HandlerInstantiatorVia
ObjectMapper.setHandlerInstantiator(HandlerInstantiator hi)aHandlerInstantiatorcan be configured for creating instances of handlers (such as serializers, deserializers, type and type id resolvers).For example this blog post (http://www.runningasroot.com/blog/2012/05/02/autowiring-jackson-deserializers-in-spring/comment-page-1/) has an example implementation of a SpringBeanHandlerInstantiator.
spring-projects-issues commentedon Nov 24, 2014
Juergen Hoeller commented
The abstract
HandlerInstantiatorclass drags in quite a few recent Jackson SPI types (at compilation time and at runtime), includingConverterwhich was introduced in Jackson 2.1... But fortunately, as of #15887, we require Jackson 2.1+ at runtime anyway.So let's give this a try, even applying it by default if it works out... As long as it can be overridden through a user-provided
HandlerInstantiator, there's nothing wrong with checking for Spring injection demand by default, just like we do for Bean Validation constraint validators.Juergen
spring-projects-issues commentedon Nov 24, 2014
Marcel Overdijk commented
Thx
@Juergenand good to see it being scheduled for 4.1.3!spring-projects-issues commentedon Dec 5, 2014
cemo koc commented
Thanks guys. :)