SpringBoot返回对象NULL转空字符串

一、使用场景

        前端要求返回的对象,如果String类型没有值要返回空字符串,没有值要数组返回空数组,然而后台直接返回的话,是NULL值,需要手动处理,为了简便配置WebMvcConfigurationSupport即可

二、实例

1、配置MyFastJsonConfig继承WebMvcConfigurationSupport

2、重写configureMessageConverters,实现返回对应的空字符串和空对象

3、重写addResourceHandlers,添加静态资源,以免访问不到

三、代码

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

/**
 * 返回值处理
 * NULL转空字符串
 * @author lyc
 */
@Configuration
public class MyFastJsonConfig extends WebMvcConfigurationSupport {

    /**
     * 使用阿里 fastjson 作为JSON MessageConverter
     * 需要fastjson依赖
     * 
     *     com.alibaba
     *     fastjson
     *     1.2.76
     * 
     * @param converters
     */

    /**
     * 源码解释:
     *  重写此方法以添加自定义 {@link HttpMessageConverter HttpMessageConverters}
     *  以与 {@link RequestMappingHandlerAdapter}
     *  和 {@link ExceptionHandlerExceptionResolver} 一起使用。
     *  

将转换器添加到列表会关闭默认情况下会注册的默认转换器。另请参阅 {@link addDefaultHttpMessageConverters} * 添加默认消息转换器。 * @param converters 将消息转换器添加到的列表(最初是一个空列表) * * * Override this method to add custom {@link HttpMessageConverter HttpMessageConverters} * to use with the {@link RequestMappingHandlerAdapter} and the * {@link ExceptionHandlerExceptionResolver}. *

Adding converters to the list turns off the default converters that would * otherwise be registered by default. Also see {@link #addDefaultHttpMessageConverters} * for adding default message converters. * @param converters a list to add message converters to (initially an empty list) */ @Override public void configureMessageConverters(List> converters) { FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter(); FastJsonConfig config = new FastJsonConfig(); config.setSerializerFeatures( // 保留map空的字段 SerializerFeature.WriteMapNullValue, // 将String类型的null转成"" SerializerFeature.WriteNullStringAsEmpty, // 将Number类型的null转成0 SerializerFeature.WriteNullNumberAsZero, // 将List类型的null转成[] SerializerFeature.WriteNullListAsEmpty, // 将Boolean类型的null转成false SerializerFeature.WriteNullBooleanAsFalse, // 避免循环引用 SerializerFeature.DisableCircularReferenceDetect); converter.setFastJsonConfig(config); converter.setDefaultCharset(Charset.forName("UTF-8")); List mediaTypeList = new ArrayList<>(); // 解决中文乱码问题,相当于在Controller上的@RequestMapping中加了个属性produces = "application/json" mediaTypeList.add(MediaType.APPLICATION_JSON); converter.setSupportedMediaTypes(mediaTypeList); converters.add(converter); } /** * 源码解释: * 覆盖此方法以添加资源处理程序以提供静态资源 * Override this method to add resource handlers for serving static resources. * @see ResourceHandlerRegistry * * 解决的问题: * 静态资源不可以访问,例如swagger页面 * * @param registry */ @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler(new String[]{"/**"}) .addResourceLocations(new String[]{"classpath:/static/"}); registry.addResourceHandler(new String[]{"doc.html"}) .addResourceLocations(new String[]{"classpath:/META-INF/resources/"}); registry.addResourceHandler(new String[]{"/webjars/**"}) .addResourceLocations(new String[]{"classpath:/META-INF/resources/webjars/"}); super.addResourceHandlers(registry); } }

四、测试

1、创建实体类

/**
 * @author lyc
 */
@Data
public class MyUser {
    private String name;
    private List subjects;
}

2、创建controller

/**
 * @author lyc
 */
@Controller
public class MyController {

    @RequestMapping("/getUser")
    public MyUser getUser(){
        return new MyUser();
    }

}

3、访问看返回值

{
  "code": "000000",
  "data": {
    "name": "",
    "subjects": []
  },
  "msg": "执行成功"
}

4、结果对比

     处理后:

SpringBoot返回对象NULL转空字符串_第1张图片

    处理前:

SpringBoot返回对象NULL转空字符串_第2张图片

 

 

你可能感兴趣的:(SpringBoot,spring,boot,java,json)