为了账号安全,请及时绑定邮箱和手机立即绑定

为什么我的春季数据页面对象的自定义 json 序列化程序未被调用

为什么我的春季数据页面对象的自定义 json 序列化程序未被调用

慕姐8265434 2022-09-14 10:44:28
这是我的对象映射器配置。我提供了我自己的对象映射器,以便覆盖Spring boot的2.0附带的对象映射器。尽管如此,我还包含了 Json 组件模块 (),以便我可以使用注释来获取自定义序列化程序。@JsonComponentpublic class ObjectMapperConfigurer {    public static ObjectMapper configureObjectMapper(ObjectMapper objectMapper) {        return objectMapper.registerModules(                // First three modules can be found here. https://github.com/FasterXML/jackson-modules-java8                new Jdk8Module(), // support for other new Java 8 datatypes outside of date/time: most notably Optional, OptionalLong, OptionalDouble                new JavaTimeModule(), // support for Java 8 date/time types (specified in JSR-310 specification)                new ParameterNamesModule(), // Support for detecting constructor and factory method ("creator") parameters without having to use @JsonProperty annotation                // These two modules are provided by spring                new JsonComponentModule(), // Enables https://docs.spring.io/spring-boot/docs/2.1.0.RELEASE/reference/htmlsingle/#boot-features-json-components                new GeoModule(), // Enables marshalling of GeoResult<T>, GeoResults<T>, and GeoPage<T>                new Hibernate5Module().enable(Hibernate5Module.Feature.FORCE_LAZY_LOADING) // Allows jackson to gracefully handle Hibernate lazy loading,        )                .setSerializationInclusion(JsonInclude.Include.NON_NULL)                // Turn on/off some features. https://github.com/FasterXML/jackson-databind/wiki/JacksonFeatures                .enable(DeserializationFeature.FAIL_ON_NUMBERS_FOR_ENUMS)                .enable(DeserializationFeature.FAIL_ON_READING_DUP_TREE_KEY)                .enable(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS)                .enable(SerializationFeature.INDENT_OUTPUT)    }
查看完整描述

2 回答

?
POPMUISE

TA贡献1765条经验 获得超5个赞

就我而言,我必须手动配置消息转换器。我将其添加到以下各项的实现中:WebMvcConfigurer


  @Override

  public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {

      Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();

      builder.serializerByType(PageImpl.class, new PageJsonSerializer());

      converters.add(new MappingJackson2HttpMessageConverter(builder.build()));

  }


查看完整回答
反对 回复 2022-09-14
?
慕尼黑的夜晚无繁华

TA贡献1864条经验 获得超6个赞

我能够通过做两件事来解决我的问题。


@Component

public class PageJsonSerializer extends StdSerializer<Page> {

扩展而不是我实际上不知道这是否是解决方案的一部分。StdSerializerJsonSerializer


我认为真正的帮助来自手动注册序列化程序,而不是依赖于 .@JsonComponent


所以我现在看起来像这样。ObjectMapperConfigurer


public class ObjectMapperConfigurer {

    public static ObjectMapper configureObjectMapper(ObjectMapper objectMapper) {

        return objectMapper.registerModules(

                // First three modules can be found here. https://github.com/FasterXML/jackson-modules-java8

                new Jdk8Module(), // support for other new Java 8 datatypes outside of date/time: most notably Optional, OptionalLong, OptionalDouble

                new JavaTimeModule(), // support for Java 8 date/time types (specified in JSR-310 specification)

                new ParameterNamesModule(), // Support for detecting constructor and factory method ("creator") parameters without having to use @JsonProperty annotation

                // Manually registering my serializer. 

                new SimpleModule().addSerializer(Page.class, pageJsonSerializer),

... all the same

}

我还从我的对象映射器中删除了 ,因为它似乎已损坏。JsonComponentModule


查看完整回答
反对 回复 2022-09-14
  • 2 回答
  • 0 关注
  • 146 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号