我想在spring MVC中用jackson序列化一个对象。春季MVC杰克逊自动序列化?
我有一个控制器,它返回一个ObjectTest1,它具有一个属性ObjectTest2。
public class ObjectTest1{
private ObjectTest2;
// setters getters...
}
public class ObjectTest2{
private String value;
// setters getters...
}
public @ResponseBody ObjectTest1 test() throws IOException ...
我有一个映射器和我有ObjectTest2串行器,我已经注释的ObjectTest1.getObjectTest2方法与@JsonSerialize(使用= ObjectTest2.class)。
它工作正常! 但我想在很多Object中使用这个序列化器,而不仅仅是在ObjectTest1中。
我应该怎么做才能避免每个getter方法都加注释?可以自动使用此序列化程序自动处理ObjectTest2的所有特性?
更新:
我已经在我的代码中使用此:
<mvc:annotation-driven>
在Ajax响应对象生成正确的JSON。也许我应该尝试解释另一种方式。
所以。 我有这些对象:
public class DTO{
private InnerThing innerThing;
@JsonSerialize(using=ThingSerializer.class)
public InnerThing getThing(){...}
}
public class InnerThing{
private String value;
}
生成JSON的样子:
{"innerThing":{"value":"something"}}
Afther当我写了一个串行,JSON是:
{"innerThing":"something"}
这是确定的,但获取json的第二个版本我必须使用@JsonSerialize注释DTO类中的getInnerThing方法...
我不想注释所有使用InnerThing作为属性的方法。 所以我的问题是,可以春天自动序列化每种属性哪种类型是InnerThing?
如果杰克逊被Spring 3类路径中,你有'MVC:annotation- ''[Spring MVC自动转换每个'@ ResponseBody'返回的对象](http://static.springsource.org/spring/docs/curren t/spring-framework-reference/htmlsingle/spring-framework-reference.html#mvc-config-enable) - 你不需要使用'@ JsonSerialize'。 – Xaerxess