亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

為什么我的春季數據頁面對象的自定義 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 關注
  • 127 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號