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

这是我的对象映射器配置。我提供了我自己的对象映射器,以便覆盖Spring boot的2.0附带的对象映射器。尽管如此,我还包含了 Json 组件模块 (),以便我可以使用注释来获取自定义序列化程序。@JsonComponent


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

                // 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)


    }


慕姐8265434
浏览 147回答 2
2回答

POPMUISE

就我而言,我必须手动配置消息转换器。我将其添加到以下各项的实现中:WebMvcConfigurer&nbsp; @Override&nbsp; public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {&nbsp; &nbsp; &nbsp; Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();&nbsp; &nbsp; &nbsp; builder.serializerByType(PageImpl.class, new PageJsonSerializer());&nbsp; &nbsp; &nbsp; converters.add(new MappingJackson2HttpMessageConverter(builder.build()));&nbsp; }

慕尼黑的夜晚无繁华

我能够通过做两件事来解决我的问题。@Componentpublic class PageJsonSerializer extends StdSerializer<Page> {扩展而不是我实际上不知道这是否是解决方案的一部分。StdSerializerJsonSerializer我认为真正的帮助来自手动注册序列化程序,而不是依赖于 .@JsonComponent所以我现在看起来像这样。ObjectMapperConfigurerpublic class ObjectMapperConfigurer {&nbsp; &nbsp; public static ObjectMapper configureObjectMapper(ObjectMapper objectMapper) {&nbsp; &nbsp; &nbsp; &nbsp; return objectMapper.registerModules(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // First three modules can be found here. https://github.com/FasterXML/jackson-modules-java8&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new Jdk8Module(), // support for other new Java 8 datatypes outside of date/time: most notably Optional, OptionalLong, OptionalDouble&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new JavaTimeModule(), // support for Java 8 date/time types (specified in JSR-310 specification)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new ParameterNamesModule(), // Support for detecting constructor and factory method ("creator") parameters without having to use @JsonProperty annotation&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Manually registering my serializer.&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new SimpleModule().addSerializer(Page.class, pageJsonSerializer),... all the same}我还从我的对象映射器中删除了 ,因为它似乎已损坏。JsonComponentModule
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java