Uploaded image for project: 'Spring Framework'
  1. Spring Framework
  2. SPR-10768

Spring dependency injection for Jackson serializers

    Details

    • Type: New Feature
    • Status: Closed
    • Priority: Major
    • Resolution: Complete
    • Affects Version/s: 3.2.3
    • Fix Version/s: 4.1.3
    • Component/s: Core
    • Labels:
    • Last commented by a User:
      true

      Description

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

        Issue Links

          Activity

          Hide
          pwebb Phil Webb added a comment -

          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?

          Show
          pwebb Phil Webb added a comment - 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?
          Hide
          cemkoc Cemo Koc added a comment - - edited

          Exactly.

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

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

          Show
          cemkoc Cemo Koc added a comment - - edited Exactly. @JsonSerialize(using = PhotoSerializer.class, include= JsonSerialize.Inclusion.NON_NULL) This is quite similar to javax.validation.Validation integration.
          Hide
          juergen.hoeller Juergen Hoeller added a comment -

          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

          Show
          juergen.hoeller Juergen Hoeller added a comment - 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
          Hide
          marceloverdijk Marcel Overdijk added a comment -

          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.

          Show
          marceloverdijk Marcel Overdijk added a comment - 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.
          Hide
          juergen.hoeller Juergen Hoeller added a comment -

          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 SPR-11262, 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

          Show
          juergen.hoeller Juergen Hoeller added a comment - 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 SPR-11262 , 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
          Hide
          marceloverdijk Marcel Overdijk added a comment -

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

          Show
          marceloverdijk Marcel Overdijk added a comment - Thx @Juergen and good to see it being scheduled for 4.1.3!
          Hide
          cemokoc cemo koc added a comment -

          Thanks guys.

          Show
          cemokoc cemo koc added a comment - Thanks guys.

            People

            • Assignee:
              sdeleuze S├ębastien Deleuze
              Reporter:
              cemkoc Cemo Koc
              Last updater:
              Juergen Hoeller
            • Votes:
              3 Vote for this issue
              Watchers:
              12 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:
                Days since last comment:
                3 years, 12 weeks, 1 day ago