Spring Boot默认的JSON解析框架设置

方案一:启动类继承WebMvcConfigurerAdapter,覆盖方法configureMessageConverters

...
@SpringBootApplication
public class UserApplication extends WebMvcConfigurerAdapter{

  @Override
  public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    super.configureMessageConverters(converters);

    // 初始化转换器
    FastJsonHttpMessageConverter fastConvert = new FastJsonHttpMessageConverter();
    // 初始化一个转换器配置
    FastJsonConfig fastJsonConfig = new FastJsonConfig();
 //用于美化格式,可注释掉   fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
    // 将配置设置给转换器并添加到HttpMessageConverter转换器列表中
    fastConvert.setFastJsonConfig(fastJsonConfig);

    converters.add(fastConvert);
  }

  public static void main(String[] args) {
    SpringApplication.run(UserApplication.class, args);
  }
}

方案二:在启动类中注入 HttpMessageConverters

...
@SpringBootApplication
public class UserApplication {
    /**
     * 配置FastJson为Spring Boot默认JSON解析框架
     * @return  HttpMessageConverters
     */
    @Bean
    public HttpMessageConverters fastJsonHttpMessageConverters() {
        // 1.定义一个converters转换消息的对象
        FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
        // 2.添加fastjson的配置信息,比如: 是否需要格式化返回的json数据
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
        // 3.在converter中添加配置信息
        fastConverter.setFastJsonConfig(fastJsonConfig);
        // 4.将converter赋值给HttpMessageConverter
        HttpMessageConverter<?> converter = fastConverter;
        // 5.返回HttpMessageConverters对象
        return new HttpMessageConverters(converter);
    }
    public static void main(String[] args) {
        SpringApplication.run(UserApplication.class, args);
    }
}

mark一下

原文地址:https://www.cnblogs.com/wujh88/p/10333785.html

时间: 2024-08-10 09:38:51

Spring Boot默认的JSON解析框架设置的相关文章

Spring Boot完美使用FastJson解析JSON数据

个人使用比较习惯的json框架是fastjson,所以spring boot默认的json使用起来就很陌生了,所以很自然我就想我能不能使用fastjson进行json解析呢?        引入fastjson依赖库: <dependencies> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1

4. 使用别的json解析框架【从零开始学Spring Boot】

转载:http://blog.csdn.net/linxingliang/article/details/51585921 此文章已经废弃,请看新版的博客的完美解决方案: 78. Spring Boot完美使用FastJson解析JSON数据[从零开始学Spring Boot] http://412887952-qq-com.iteye.com/blog/2315202

json解析框架

2.需要在App.java中继承WebMvcConfigurerAdapter重写方法:configureMessageConverters 添加我们自己定义的json解析框架; 2.1 @Bean注入第三方的json解析框架: @Bean public HttpMessageConverters fastJsonHttpMessageConverters() { // 1.需要先定义一个 convert 转换消息的对象; FastJsonHttpMessageConverter fastCon

Spring boot 默认静态资源路径与手动配置访问路径的方法

这篇文章主要介绍了Spring boot 默认静态资源路径与手动配置访问路径的方法,非常不错,具有参考借鉴价值,需要的朋友可以参考下 在application.propertis中配置 ##端口号 server.port=8081 ##默认前缀 spring.mvc.view.prefix=/ ## 响应页面默认后缀 spring.mvc.view.suffix=.html # 默认值为 /** spring.mvc.static-path-pattern=/** # 这里设置要指向的路径,多个

Spring Boot 默认指标从哪来?

了解有关 Spring Boot 默认指标及其来源的更多信息. 您是否注意到 Spring Boot 和 Micrometer 为您的应用生成的所有默认指标? 如果没有 - 您可以将 actuator 依赖项添加到项目中,然后点击 / actuator / metrics 端点,在那里您将找到有关 JVM .进程.Tomcat.流量等的有用信息. 然后,添加一些缓存,数据源 或 JPA 依赖项,甚至会出现更多指标.如果您想知道它们是如何结束的,我们可以在哪里找到关于它们所描述的参数的解释,那么这

Spring Boot 默认的指标数据从哪来的?

了解有关 Spring Boot 默认指标及其来源的更多信息. 您是否注意到 Spring Boot 和 Micrometer 为您的应用生成的所有默认指标?如果没有 - 您可以将 actuator 依赖项添加到项目中,然后点击 / actuator / metrics 端点,在那里您将找到有关 JVM .进程.Tomcat.流量等的有用信息.然后,添加一些缓存,数据源 或 JPA 依赖项,甚至会出现更多指标.如果您想知道它们是如何结束的,我们可以在哪里找到关于它们所描述的参数的解释,那么这篇文

Spring Boot 启动源码解析系列六:执行启动方法一

1234567891011121314151617181920212223242526272829303132333435363738394041424344 public ConfigurableApplicationContext (String... args) { StopWatch stopWatch = new StopWatch(); // 开始执行,记录开始时间 stopWatch.start(); ConfigurableApplicationContext context =

Spring Boot中使用FastJson解析Json数据

首先我们创建一个maven工程,如下图: 第二步:配置pom.xml <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.1.RELEASE</version> </parent> <properties> <p

Java基础知识强化103:JSON解析框架汇总

1.Gson Gson是Google提供的一个能够将Java对象转换成相应JSON表达形式的一个开源Java类库,当然用Gson也能将JSON字符串转换成与之等价的Java对象.Gson对于任何Java对象都有效,包括那些预先存在没有源代码的对象. 现在已经有一些能将Java对象转换成JSON的开源项目了.但是大多数项目都要求你在类文件中加入Java注解,而当你无法改动源代码的时候这是无法做到的.并且它们也不支持Java泛型.但是Gson却将这两点作为自己非常重要的设计目标. 目标 使用toJs