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-issuemaster opened this Issue Jul 22, 2013 · 7 comments

Comments

Projects
None yet
2 participants
@spring-issuemaster
Copy link
Collaborator

spring-issuemaster 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:

  • #15887 Require Jackson 2.1+, Quartz 2.1.4+, EhCache 2.5+
  • #11715 Allow usage of ConversionService for Jackson HttpMessageConverter
  • #18919 SpringHandlerInstantiator should support all HandlerInstantiator-provided factory methods

3 votes, 12 watchers

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

spring-issuemaster commented Jul 22, 2013

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-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

spring-issuemaster commented Jul 22, 2013

Cemo Koc commented

Exactly.

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

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

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

spring-issuemaster commented 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-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

spring-issuemaster commented 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.
@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-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

spring-issuemaster 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-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

spring-issuemaster commented Nov 24, 2014

Marcel Overdijk commented

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

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

spring-issuemaster commented Dec 5, 2014

cemo koc commented

Thanks guys. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment