添加Hibernate4Module会破坏序列化

时间:2017-05-22 14:17:21

标签: json spring hibernate jackson

我在添加Hibernate4Module以支持延迟对象序列化后面临一个问题。

我的配置文件:

 @EnableWebMvc
 @ServletComponentScan(basePackages = "my.backend")
 @EnableAutoConfiguration(exclude = MultipartAutoConfiguration.class)
 @Configuration
 @EnableSwagger2
 public class MyConfiguration extends WebMvcConfigurerAdapter{

     @Override
     public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
         configurer.favorPathExtension(false).
            favorParameter(true).
            parameterName("mediaType").
            ignoreAcceptHeader(true).
            useJaf(false).
            defaultContentType(MediaType.APPLICATION_JSON).
            mediaType("xml", MediaType.APPLICATION_XML).
            mediaType("json", MediaType.APPLICATION_JSON);
     }

/* Here we register the Hibernate4Module into an ObjectMapper, then set this custom-configured ObjectMapper
 * to the MessageConverter and return it to be added to the HttpMessageConverters of our application*/
     public MappingJackson2HttpMessageConverter jacksonMessageConverter(){
         MappingJackson2HttpMessageConverter messageConverter = new MappingJackson2HttpMessageConverter();
         ObjectMapper current = messageConverter.getObjectMapper();
         //Registering Hibernate4Module to support lazy objects
         current.registerModule(new Hibernate4Module());

         messageConverter.setObjectMapper(current);
         return messageConverter;

     }

     @Override
     public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
         //Here we add our custom-configured HttpMessageConverter
         converters.add(jacksonMessageConverter());
         super.configureMessageConverters(converters);
     }
 }

Hibernate实体被串行化了。 问题在于普通的jsons,它存储在DB中或以任何其他方式生成。 例如

 String myJson = {"myField":"2123456"}

以邮件格式返回

 "{\"myField\":"2123456"}

看起来有些默认模块坏了。任何人都可以提出一些建议吗?

0 个答案:

没有答案