如何解析Json数据 | 带你读《SpringBoot实战教程》之十二

简介: 本节介绍使用FastJson解析Json数据。

上一篇:详解访问静态资源 | 带你读《SpringBoot实战教程》之十一
下一篇:定义全局异常处理器 | 带你读《SpringBoot实战教程》之十三

本文来自于千锋教育在阿里云开发者社区学习中心上线课程《SpringBoot实战教程》,主讲人杨红艳,点击查看视频内容

使用FastJson解析Json数据

SpringBoot默认配置的是Jackson。
自定义使用FastJson解析Json数据,添加依赖:

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

配置FastJson有两种方式:
第一种:让启动类继承WebMvcConfigurerAdapter

public class SpringApplications extends WebMvcConfigurerAdapter {

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        //创建FastJSON的消息转换器
        FastJsonHttpMessageConverter convert = new FastJsonHttpMessageConverter();
        //创建FastJSON的配置对象
        FastJsonConfig config = new FastJsonConfig();
       //对Json数据进行格式化
        config.setSerializerFeatures(SerializerFeature.PrettyFormat);

        convert.setFastJsonConfig(config);
        converters.add(convert);
    }
}

创建一个Person的实体类:

public class Person {

    private int id;
    private String name;
    private Date date;

    //set,get方法略
}

Controller:

@Controller
public class TestController {

    @RequestMapping("/person")
    @ResponseBody
    public Object show() {

        Person ren = new Person();
        ren.setId(66);
        ren.setName("赵六");
        ren.setDate(new Date());
        return ren;
    }  

}

在启动类中添加需要扫描的包:

@SpringBootApplication(scanBasePackages="com.qianfeng.controller")

执行结果:
image.png

乱码解决:把springboot的response编码设置为utf-8这个功能开启就好。
全局配置文件中添加:

spring.http.encoding.force=true

image.png
在定义的date上添加注解,来确定是使用FastJson解析Json数据的。

    @JSONField(format="yyyy-MM-dd HH")

image.png

第二种:@Bean注入

    @Bean
    public HttpMessageConverters fastJsonHttpMessageConverters()
    {
        FastJsonHttpMessageConverter convert = new FastJsonHttpMessageConverter();
        FastJsonConfig config = new FastJsonConfig();
        config.setSerializerFeatures(SerializerFeature.PrettyFormat);
        convert.setFastJsonConfig(config);
        
        HttpMessageConverter<?> con=convert;
        return new HttpMessageConverters(con);
    }

执行结果:
image.png

19.自定义拦截器

有些时候我们需要自已配置SpringMVC而不是采用默认,比如说增加一个拦截器,这个时候就得通过继承WebMvcConfigurerAdapter然后重写父类中的方法进行扩展。

首先创建一个定义拦截器的包:
image.png
在下面创建一个拦截器
image.png
自行定义拦截器:

@Configuration
public class MyInterceptor extends WebMvcConfigurerAdapter {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        HandlerInterceptor handlerInterceptor=new HandlerInterceptor() {
            
            @Override
            public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2) throws Exception {
                System.out.println("自定义拦截器.....");
                return true;
            }
            
            @Override
            public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
                    throws Exception {
                // TODO Auto-generated method stub
                
            }
            
            @Override
            public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
                    throws Exception {
                // TODO Auto-generated method stub
                
            }
        };
        
        registry.addInterceptor(handlerInterceptor).addPathPatterns("/**");
    }

我们需要SpringBoot扫描到这个拦截器,所有需要在此处指明所在的包:

@SpringBootApplication(scanBasePackages={"com.qianfeng.controller","com.qianfeng.interceptor"})

测试拦截器是否好用,我们通过访问路径,看控制台是否打印了这个自定义拦截器,说明拦截器起作用了。
image.png

相关文章
|
6月前
|
JSON 缓存 自然语言处理
多语言实时数据微店商品详情API:技术实现与JSON数据解析指南
通过以上技术实现与解析指南,开发者可高效构建支持多语言的实时商品详情系统,满足全球化电商场景需求。
|
6月前
|
人工智能 Java 开发者
【Spring】原理解析:Spring Boot 自动配置
Spring Boot通过“约定优于配置”的设计理念,自动检测项目依赖并根据这些依赖自动装配相应的Bean,从而解放开发者从繁琐的配置工作中解脱出来,专注于业务逻辑实现。
2258 0
|
7月前
|
监控 Java API
Spring Boot 3.2 结合 Spring Cloud 微服务架构实操指南 现代分布式应用系统构建实战教程
Spring Boot 3.2 + Spring Cloud 2023.0 微服务架构实践摘要 本文基于Spring Boot 3.2.5和Spring Cloud 2023.0.1最新稳定版本,演示现代微服务架构的构建过程。主要内容包括: 技术栈选择:采用Spring Cloud Netflix Eureka 4.1.0作为服务注册中心,Resilience4j 2.1.0替代Hystrix实现熔断机制,配合OpenFeign和Gateway等组件。 核心实操步骤: 搭建Eureka注册中心服务 构建商品
1162 3
|
8月前
|
存储 JSON 关系型数据库
【干货满满】解密 API 数据解析:从 JSON 到数据库存储的完整流程
本文详解电商API开发中JSON数据解析与数据库存储的全流程,涵盖数据提取、清洗、转换及优化策略,结合Python实战代码与主流数据库方案,助开发者构建高效、可靠的数据处理管道。
|
7月前
|
JSON 算法 API
淘宝商品评论API接口核心解析,json数据返回
淘宝商品评论API是淘宝开放平台提供的数据服务接口,允许开发者通过编程方式获取指定商品的用户评价数据,包括文字、图片、视频评论及评分等。其核心价值在于:
|
5月前
|
监控 Cloud Native Java
Spring Boot 3.x 微服务架构实战指南
🌟蒋星熠Jaxonic,技术宇宙中的星际旅人。深耕Spring Boot 3.x与微服务架构,探索云原生、性能优化与高可用系统设计。以代码为笔,在二进制星河中谱写极客诗篇。关注我,共赴技术星辰大海!(238字)
1054 2
Spring Boot 3.x 微服务架构实战指南
|
5月前
|
前端开发 Java 微服务
《深入理解Spring》:Spring、Spring MVC与Spring Boot的深度解析
Spring Framework是Java生态的基石,提供IoC、AOP等核心功能;Spring MVC基于其构建,实现Web层MVC架构;Spring Boot则通过自动配置和内嵌服务器,极大简化了开发与部署。三者层层演进,Spring Boot并非替代,而是对前者的高效封装与增强,适用于微服务与快速开发,而深入理解Spring Framework有助于更好驾驭整体技术栈。
|
6月前
|
消息中间件 Ubuntu Java
SpringBoot整合MQTT实战:基于EMQX实现双向设备通信
本教程指导在Ubuntu上部署EMQX 5.9.0并集成Spring Boot实现MQTT双向通信,涵盖服务器搭建、客户端配置及生产实践,助您快速构建企业级物联网消息系统。
2396 1
|
5月前
|
JSON Java Go
【GoGin】(2)数据解析和绑定:结构体分析,包括JSON解析、form解析、URL解析,区分绑定的Bind方法
bind或bindXXX函数(后文中我们统一都叫bind函数)的作用就是将,以方便后续业务逻辑的处理。
402 3
|
5月前
|
XML JSON Java
【SpringBoot(三)】从请求到响应再到视图解析与模板引擎,本文带你领悟SpringBoot请求接收全流程!
Springboot专栏第三章,从请求的接收到视图解析,再到thymeleaf模板引擎的使用! 本文带你领悟SpringBoot请求接收到渲染的使用全流程!
442 3

热门文章

最新文章

推荐镜像

更多
  • DNS