首页 > 其他分享 >to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS

to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS

时间:2023-01-04 23:55:15浏览次数:76  
标签:exception SerializationFeature springframework disable BEANS import MappingJacks

to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS

解决方案
参考网上的解决方案,通过重写WebMvcConfigurationSupport中的configureMessageConverters方法按照报错信息的提示进行更改,具体代码如下:

package com.hye.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import java.util.List;

@Configuration
public class GlobalConfig extends WebMvcConfigurationSupport {
    @Override
    protected void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        super.configureMessageConverters(converters);
        converters.add(mappingJackson2HttpMessageConverter());
    }

    // 解决序列化空对象问题
    private MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
        ObjectMapper mapper = new ObjectMapper();
        // 关键代码
        mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
        MappingJackson2HttpMessageConverter converter =
                new MappingJackson2HttpMessageConverter(mapper);
        return converter;
    }
}

标签:exception,SerializationFeature,springframework,disable,BEANS,import,MappingJacks
From: https://www.cnblogs.com/echohye/p/17026348.html

相关文章