由JSON序列化失敗引發的對序列化過程配置的思考

問題描述

本次遇到問題時所使用的框架是Spring Boot,處理完請求以後,返回數據以前,在POJO轉化成JSON時,有些屬性違背輸出規則或者有些屬性循環引用會形成沒法輸出。java

報錯信息:org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: No serializer found for class java.lang.Object and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS); nested exception is com.fasterxml.jackson.databind.JsonMappingException: No serializer found for class java.lang.Object and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS)spring

部分代碼片斷:json

@Override
    public ReviewReportDto getReviewReport(String requestId) {
        ReviewReportDto resDto = new ReviewReportDto();
        ...
        ...
        //Object對象實例致使序列化錯誤
        resDto.setOffLineDto(new Object());
        return resDto;
    }

固然解決方式有不少種,在最第一版本的代碼開發過程當中,因爲部分業務的缺失,暫時把空位補上new Object(),而其表示的其實就是null(空對象),致使序列化時報錯。數組


解決方法

  • 第一種方法:
    直接把new Object()替換爲null,便可解決序列化錯誤問題。
  • 第二種方法
    經過設置,使Jackson遇到空對象不拋異常。
@Bean("objectMapper")
    public ObjectMapper myMapper() {
        return new ObjectMapper().disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
    }
  • 第三種方法(spring mvc項目)
    經過xml配置自定義類來覆蓋默認配置
//自定義類
    public class MyMapper extends ObjectMapper {
        public MyMapper() {
            configure(SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS, false);
        }
    }
<!-- xml-springMVC 配置 -->
    <mvc:annotation-driven>
        <mvc:message-converters>
            ...
            <bean     class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
                ...
                <!-- 配置自定義的objectMapper  -->
                <property name="objectMapper">
                    <bean class="com.myPackage.MyMapper">
                    </bean>
                </property>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>

序列化過程配置的規整

此處貼出SerializationFeature的源碼,以及對部分經常使用配置方式的總結:mvc

package com.fasterxml.jackson.databind;

import com.fasterxml.jackson.databind.cfg.ConfigFeature;

public enum SerializationFeature implements ConfigFeature {

    //是否環繞根元素,默認false,若是爲true,則默認以類名做爲根元素。根元素名稱也能夠經過@JsonRootName來自定義。
    WRAP_ROOT_VALUE(false),

    //是否縮放排列輸出,默認false,有些場合爲了便於排版閱讀則須要對輸出作縮放排列
    INDENT_OUTPUT(false),

    //遇到空對象則失敗
    FAIL_ON_EMPTY_BEANS(true),

    //自我引用則失敗
    FAIL_ON_SELF_REFERENCES(true),

    //com.fasterxml.jackson.annotation.JsonUnwrapped包下相關聯的類將會拋出異常
    WRAP_EXCEPTIONS(true),


    FAIL_ON_UNWRAPPED_TYPE_IDENTIFIERS(true),
    CLOSE_CLOSEABLE(false),
    FLUSH_AFTER_WRITE_VALUE(true),
    WRITE_DATES_AS_TIMESTAMPS(true),

    //序列化日期時以timestamps輸出,默認true
    WRITE_DATE_KEYS_AS_TIMESTAMPS(false),

    WRITE_DATES_WITH_ZONE_ID(false),
    WRITE_DURATIONS_AS_TIMESTAMPS(true),

    //序列化char[]時以json數組輸出,默認false
    WRITE_CHAR_ARRAYS_AS_JSON_ARRAYS(false),

    //序列化枚舉是以toString()來輸出,默認false,即默認以name()來輸出
    WRITE_ENUMS_USING_TO_STRING(false),

    //序列化枚舉是以ordinal()來輸出,默認false
    WRITE_ENUMS_USING_INDEX(false),

    WRITE_NULL_MAP_VALUES(true),
    /** @deprecated */
    @Deprecated
    WRITE_EMPTY_JSON_ARRAYS(true),

    //序列化單元素數組時不以數組來輸出,默認false
    WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED(false),

    /** @deprecated */
    @Deprecated
    //序列化BigDecimal時之間輸出原始數字仍是科學計數,默認false,便是否以toPlainString()科學計數方式來輸出
    WRITE_BIGDECIMAL_AS_PLAIN(false),
    WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS(true),

    //序列化Map時對key進行排序操做,默認false
    ORDER_MAP_ENTRIES_BY_KEYS(false),

    EAGER_SERIALIZER_FETCH(true),
    USE_EQUALITY_FOR_OBJECT_ID(false);

    private final boolean _defaultState;
    private final int _mask;

    private SerializationFeature(boolean defaultState) {
        this._defaultState = defaultState;
        this._mask = 1 << this.ordinal();
    }

    public boolean enabledByDefault() {
        return this._defaultState;
    }

    public int getMask() {
        return this._mask;
    }

    public boolean enabledIn(int flags) {
        return (flags & this._mask) != 0;
    }
}

By the way:引用請標明出處。若有錯誤,請批評指正。app

相關文章
相關標籤/搜索