为Spring Data REST注册Jackson模块 [英] Registering a Jackson module for Spring Data REST

查看:131
本文介绍了为Spring Data REST注册Jackson模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于Spring Data REST的工作项目示例项目,我正在尝试使用基于此 Jackson模块进行自定义序列化data-rest / wiki / Adding-custom-%28de%29serializers-to-Jackson%27s-ObjectMapper> wiki页面。

I have a working project based on the Spring Data REST example project, and I'm trying to do custom serialization using a Jackson module based on this wiki page.

这是我的杰克逊模块:

public class CustomModule extends SimpleModule {
    public static Logger logger = LoggerFactory.getLogger(CustomModule.class);

    public CustomModule() {
        super("CustomModule", new Version(1, 0, 0, null));
    }

    @Override
    public void setupModule(SetupContext context) {
        logger.debug("CustomModule.setupModule");
        SimpleSerializers simpleSerializers = new SimpleSerializers();
        simpleSerializers.addSerializer(new CustomDateTimeSerializer());
        context.addSerializers(simpleSerializers);
    }

}

wiki页面说:


在ApplicationContext范围内声明的任何Module bean都将被导出器选中并在其ObjectMapper中注册。

Any Module bean declared within the scope of your ApplicationContext will be picked up by the exporter and registered with its ObjectMapper.

我还是Spring的新手,所以我可能只是把我的模块bean定义放在错误的地方;目前它位于 src / main / resources / META-INF / spring-data-rest / shared.xml ,它是从 repositories-export导入的。 xml

I'm still pretty new to Spring, so I might just be putting my module bean definition in the wrong place; currently it's in src/main/resources/META-INF/spring-data-rest/shared.xml, which is imported from repositories-export.xml:

<bean id="customModule" class="org.hierax.wpa.schema.mapping.CustomModule" />

我在 setupModule ,但我确实看到同一个包中其他类的日志输出。

I don't see the log statement in setupModule, but I do see log output for other classes in the same package.

我正在使用Spring Data REST 1.0.0.RC2。

I'm using Spring Data REST 1.0.0.RC2.

推荐答案

我已成功使用您链接到的wiki条目中概述的解决方案(尽管自此堆栈溢出后可能已经更改)

I've had success using the solution outlined in the wiki entry that you have linked to (although perhaps it has changed since this stack overflow post)

在我的实例中,我使用的是spring-data-rest-webmvc@1.0.0.RELEASE

In my instance I was using spring-data-rest-webmvc@1.0.0.RELEASE

你的代码似乎是正确的,只要你的应用程序上下文正确加载我没有看到任何理由不能正常工作。

Your code seems to be correct and provided that your application context is being loaded correctly I don't see any reason for it not to be working.

我附上了我的简单示例使用日期格式化程序的模块:

I've attached my simpler Module which exemplifies the use of a date formatter:

@Component
public class JsonMarshallingConfigModule extends SimpleModule {

  public JsonMarshallingConfigModule() {
    super("JsonMarshallingConfigModule", new Version(1, 0, 0, "SNAPSHOT"));
  }

  @Override public void setupModule(SetupContext context) {
    context.getSerializationConfig().setDateFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm'Z'"));
  }
}

也许它可以用来概述它是否是实际的杰克逊模块是问题还是spring-data-rest-mvcweb。

Perhaps it can be used to outline if it is infact the jackson module that is the problem or spring-data-rest-mvcweb.

这篇关于为Spring Data REST注册Jackson模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆