1

我想在spring MVC中用jackson序列化一个对象。

我有一个控制器,它返回一个 ObjectTest1,它有一个属性 ObjectTest2。

public class ObjectTest1{
 private ObjectTest2;
 // setters getters...
}

public class ObjectTest2{
 private String value;
 // setters getters...
}

public @ResponseBody ObjectTest1 test() throws IOException ...

我有一个映射器,我有一个 ObjectTest2 的序列化程序,我已经用 @JsonSerialize(using = ObjectTest2.class) 注释了 ObjectTest1.getObjectTest2 方法。

它工作正常!但我想在很多对象中使用这个序列化程序,而不仅仅是在 ObjectTest1 中。

我应该怎么做才能避免在每个 getter 方法中添加注释?可以为ObjectTest2的所有属性自动使用spring这个序列化程序吗?

更新:

我已经在我的代码中使用了它:

<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"}}

在我编写了一个序列化程序之后,json 是:

{"innerThing":"something"}

没关系,但要获得第二个版本的 json,我必须用 @JsonSerialize 注释 DTO 类中的 getInnerThing 方法...

我不想注释我使用 InnerThing 作为属性的所有方法。所以我的问题是,spring 可以自动序列化 InnerThing 类型的每个属性吗?

4

2 回答 2

3

默认情况下,如果您将 Jackson 添加到类路径并使用<mvc:annotation-driven>或,Spring 将自动处理 JSON 的序列化和反序列化@EnableWebMvc

Spring 参考文档的链接:

Spring 3.0:<mvc:annotation-driven>
Spring 3.1:<mvc:annotation-driven> 和@EnableWebMvc

于 2012-09-17T18:44:12.703 回答
2

您希望 Jackson 始终使用您的自定义 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));
    }
}

组件序列化器类:

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;
    }
}
于 2012-09-20T22:42:21.550 回答