【问题标题】:Spring MVC jackson auto serialize?Spring MVC杰克逊自动序列化?
【发布时间】:2012-09-10 00:30:50
【问题描述】:

我想在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 类型的每个属性吗?

【问题讨论】:

标签: spring-mvc jackson


【解决方案1】:

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

Spring 参考文档的链接:

春季 3.0:<mvc:annotation-driven>
春季 3.1:<mvc:annotation-driven> and @EnableWebMvc

【讨论】:

    【解决方案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));
        }
    }
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-17
      • 2017-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-26
      相关资源
      最近更新 更多