3

I would like to create some serializers which are aware of some of my spring beans. The best way seems so far to me is extending DefaultSerializerProvider but I could not be sure.

@Component
public class PhotoSerializer extends JsonSerializer<String> {

   @Autowired
   Service service;

   @Override
   public void serialize(String value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException {
      jgen.writeString(service.doIt(value));
   }
}

What is the correct way of creating some serializers by Spring?

4

1 回答 1

1

您可以使用 Jackson HandlerInstantiator 在 spring 和 jackson 序列化程序之间架起一座桥梁。

这里有很好的描述:

http://www.runningasroot.com/blog/2012/05/02/autowiring-jackson-deserializers-in-spring/

于 2014-04-01T13:47:47.040 回答