Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spring dependency injection for Jackson serializers [SPR-10768] #15394

Closed
spring-projects-issues opened this issue Jul 22, 2013 · 7 comments
Closed
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

spring-projects-issues commented Jul 22, 2013

Cemo Koc opened SPR-10768 and commented

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));
   }
}

Affects: 3.2.3

Issue Links:

3 votes, 12 watchers

@spring-projects-issues
Copy link
Collaborator Author

Phil Webb commented

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?

@spring-projects-issues
Copy link
Collaborator Author

Cemo Koc commented

Exactly.

@JsonSerialize(using = PhotoSerializer.class, include= JsonSerialize.Inclusion.NON_NULL)

This is quite similar to javax.validation.Validation integration.

@spring-projects-issues
Copy link
Collaborator Author

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
Copy link
Collaborator Author

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.
@Jurgen I 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 HandlerInstantiator as this seems exactly how to this from Jackson documentation: http://wiki.fasterxml.com/HandlerInstantiator
Via ObjectMapper.setHandlerInstantiator(HandlerInstantiator hi) a HandlerInstantiator can 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
Copy link
Collaborator Author

spring-projects-issues commented Nov 24, 2014

Juergen Hoeller commented

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.

Juergen

@spring-projects-issues
Copy link
Collaborator Author

Marcel Overdijk commented

Thx @Juergen and good to see it being scheduled for 4.1.3!

@spring-projects-issues
Copy link
Collaborator Author

cemo koc commented

Thanks guys. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

2 participants