spring boot 接口返回json 格式化

浏览器可以配置插件,格式化接口返回的json。

spring boot也可以做对应配置 使浏览器访问api的json结果在页面格式化形式展示

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.WebMvcConfigurationSupport;

import java.util.ArrayList;
import java.util.List;

@Configuration
public class FastJsonConfiguration extends WebMvcConfigurationSupport {
    @Override
    public void configureMessageConverters(List> converters) {
        super.configureMessageConverters(converters);
        // 创建FastJson消息转换器
        FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
        // 创建配置类
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        // 修改配置返回内容的过滤
        fastJsonConfig.setSerializerFeatures(
            SerializerFeature.PrettyFormat
        );
         
        List fastMediaType = new ArrayList<>();
        fastMediaType.add(MediaType.APPLICATION_JSON_UTF8);
        fastJsonHttpMessageConverter.setSupportedMediaTypes(fastMediaType);
        fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);
        // 将FastJson添加到视图消息转换器列表内
        converters.add(fastJsonHttpMessageConverter);
    }

}

你可能感兴趣的:(java)