Springboot 使用其他JSON解析框架

Springboot 默认使用 jackson 作为序列化与反序列化的 JSON 框架,当我们想要使用自己更习惯的 JSON 序列化框架时,有以下三种方式:

先在 pom.xml 文件中添加 JSON 框架依赖,以下以 fastjson 为例

<!--fastjson-->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.15</version>
</dependency>

方式一:(该方式不建议使用,WebMvcConfigurerAdapter 已过期)
1.启动类继承 WebMvcConfigurerAdapter 类
2.重写 configureMessageConverters() 方法

@SpringBootApplication
public class WxSpringbootExerApplication extends WebMvcConfigurerAdapter  {

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

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
        fastJsonHttpMessageConverter.setFeatures (SerializerFeature.PrettyFormat);
        converters.add(fastJsonHttpMessageConverter);
    }
}

方式二:
1、配置类实现 WebMvcConfigurer 接口
2、重写 configureMessageConverters() 方法

@Configuration
public class WxConfig implements WebMvcConfigurer {
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
        fastJsonHttpMessageConverter.setFeatures(SerializerFeature.PrettyFormat);
        converters.add(fastJsonHttpMessageConverter);
    }
}

方式三:
在配置类中注入 HttpMessageConverters

@Configuration
public class WxConfig {

    @Bean
    public HttpMessageConverters httpMessageConverters() {
        return new HttpMessageConverters(new FastJsonHttpMessageConverter());
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot是一个基于Spring框架的快速开发框架,它提供了很多便捷的配置和快速的开发环境。在Spring Boot中,我们可以使用@RestController注解来创建一个RESTful风格的Web服务,它支持返回JSON格式的数据。 JSON是一种轻量级的数据交换格式,由于其简单、易读、易解析的特点,被广泛应用于Web应用程序中。在Spring Boot中,我们可以通过以下步骤来实现JSON数据格式化: 1. 导入相关依赖 在pom.xml文件中添加以下依赖: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> </dependency> ``` 2. 创建一个Controller类 在Controller类中定义一个返回JSON格式数据的接口,例如: ``` @RestController @RequestMapping("/user") public class UserController { @GetMapping("/{id}") public User getUser(@PathVariable Long id) { User user = new User(); user.setId(id); user.setName("张三"); user.setAge(20); return user; } } ``` 其中,@RestController注解表示该类是一个RESTful风格的控制器类,@RequestMapping注解表示请求的路径为"/user",@GetMapping注解表示该接口是一个GET请求。 3. 创建一个实体类 在实体类中定义需要返回的数据,例如: ``` public class User { private Long id; private String name; private Integer age; // getter和setter方法省略 } ``` 4. 测试接口 启动应用程序,并访问"http://localhost:8080/user/1",可以看到返回的数据是一个JSON格式的字符串,例如: ``` {"id":1,"name":"张三","age":20} ``` 以上就是基于Spring Boot框架实现JSON数据格式化的过程。通过使用@RestController注解,我们可以快速地创建一个RESTful风格的Web服务,并返回JSON格式的数据,从而方便地与前端进行数据交互。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值