2012-09-17 27 views
0

我想在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?

+0

如果杰克逊被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

回答

2

你想杰克逊一直使用自定义JsonSerializer或JsonDeserializer序列化/反序列化特定类型的?

我最终编写了一个自定义的Jackson模块,让Jackson找到了Spring bean的序列化器和反序列化器。 我使用Spring 3.1.2和Jackson 2.0。6

简体版:

public class MyObjectMapper extends ObjectMapper { 
    @Autowired 
    public MyObjectMapper(ApplicationContext applicationContext) { 
     SpringComponentModule sm = new SpringComponentModule(applicationContext); 
     registerModule(sm); 
    } 
} 

模块:

public class SpringComponentModule extends Module { 
    private ApplicationContext applicationContext; 
    public SpringComponentModule(ApplicationContext applicationContext) { 
     this.applicationContext = applicationContext; 
    } 
    @Override public String getModuleName() { 
     return "jackson-spring-component"; 
    } 
    @Override public Version version() { 
     return SpringComponentModuleVersion.instance.version(); 
    } 
    @Override 
    public void setupModule(SetupContext context) { 
     context.addSerializers(new SpringComponentSerializers(this.applicationContext)); 
     context.addDeserializers(new SpringComponentDeserializers(this.applicationContext)); 
    } 
} 

ComponentSerializer类:

public class SpringComponentSerializers extends Serializers.Base { 
    private ApplicationContext applicationContext; 
    public SpringComponentSerializers(ApplicationContext applicationContext) { 
     this.applicationContext = applicationContext; 
    } 
    @Override 
    public JsonSerializer<?> findSerializer(SerializationConfig config, JavaType type, BeanDescription beanDesc) { 
     Class<?> raw = type.getRawClass(); 
     Map<String,JsonSerializer> beanSet = applicationContext.getBeansOfType(JsonSerializer.class); 
     for(String beanName : beanSet.keySet()) { 
      JsonSerializer<?> serializer = beanSet.get(beanName); 
      if(serializer.handledType().isAssignableFrom(raw)) { 
       return serializer; 
      } 
     } 
     return null; 
    } 
}