Jackson custom serialization under Spring 3 MVC Jackson custom serialization under Spring 3 MVC json json

Jackson custom serialization under Spring 3 MVC


The problem is that Jackson does not see the annotations on getItems() if it is a service end point method; it is typically only passed type List<Item> that Spring determines. With JAX-RS (like Jersey), annotations associated with that method are passed, however (and perhaps Spring has some way as well); although it then requires bit more support from integration code (for JAX-RS, Jackson JAX-RS JSON provider module) to pass that along.

It might be easier to actually create a separate POJO (and not pass List types) so that you can add necessary annotations.

If you were using Jackson directly, you could also use ObjectWriter and specify default date format to use. But I don't know if Spring allows you to do that (most frameworks do not and only expose configurability of ObjectMapper).

One more note -- instead of custom serializer (and/or deserializer), you can also use simple annotations with Dates (and on Jackson 2.x):

public class DateStuff {  @JsonFormat(shape=JsonFormat.Shape.STRING, pattern="'Date:'yyyy'-'MM'-'dd")  public Date date;}

to specify per-property format override.