为Spring Data REST注册Jackson模块

Mik*_*dge 7 jackson spring-data-rest

我有一个基于Spring Data REST 示例项目的工作项目,我正在尝试使用基于此Wiki页面Jackson模块进行自定义序列化.

这是我的杰克逊模块:

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

}
Run Code Online (Sandbox Code Playgroud)

wiki页面说:

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

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

<bean id="customModule" class="org.hierax.wpa.schema.mapping.CustomModule" />
Run Code Online (Sandbox Code Playgroud)

我没有看到日志语句setupModule,但我确实看到同一个包中其他类的日志输出.

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

Den*_*edo 7

目前,可以在Spring Boot中自定义模块,如下所示:

@Bean
public Module customModule() {
  return new CustomModule();
}
Run Code Online (Sandbox Code Playgroud)

参考:Spring最新的Jackson集成改进


Ben*_*lan 5

使用您链接到的Wiki条目中概述的解决方案,我已经取得了成功(尽管自从该堆栈溢出后可能已经更改了)

以我为例,我使用的是spring-data-rest-webmvc@1.0.0.RELEASE

您的代码似乎是正确的,并且假设您的应用程序上下文已正确加载,但我看不出任何原因使其无法正常工作。

我附上了更简单的模块,该模块举例说明了日期格式化程序的使用:

@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'"));
  }
}
Run Code Online (Sandbox Code Playgroud)

也许可以用来概述问题所在的杰克逊模块或spring-data-rest-mvcweb。