SpringBoot2:请求处理原理分析-利用内容协商功能实现接口的两种数据格式(JSON、XML)

文章目录

一、功能说明

我们知道,用@ResponseBody注解标注的接口,默认返回给页面的是json数据。

其实,也可以返回xml结构的数据给页面。

这一篇就来实现一下这个小功能。

二、案例实现

pom

xml 复制代码
        <dependency>
            <groupId>com.fasterxml.jackson.dataformat</groupId>
            <artifactId>jackson-dataformat-xml</artifactId>
        </dependency>

1、基于请求头实现

直接添加了pom即可,无需其他配置。

获取json数据:application/json

获取xml数据:application/xml

2、基于请求参数实现

yaml配置

yaml 复制代码
spring:
  mvc:
    contentnegotiation:
      favor-parameter: true

获取json数据:http://127.0.0.1:8080/test/person?format=json

获取xml数据:http://127.0.0.1:8080/test/person?format=xml

相关推荐
+VX:Fegn08953 小时前
计算机毕业设计|基于springboot + vue在线音乐播放系统(源码+数据库+文档)
数据库·vue.js·spring boot·后端·课程设计
+VX:Fegn08954 小时前
计算机毕业设计|基于springboot + vue律师咨询系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·后端·课程设计
困知勉行19855 小时前
springboot整合redis
java·spring boot·redis
中年程序员一枚6 小时前
Springboot报错Template not found For name “java/lang/Object_toString.sql
java·spring boot·python
fanruitian7 小时前
Springboot项目父子工程
java·数据库·spring boot
super_lzb7 小时前
mybatis拦截器ParameterHandler详解
java·数据库·spring boot·spring·mybatis
JavaGuru_LiuYu11 小时前
Spring Boot 整合 SSE(Server-Sent Events)
java·spring boot·后端·sse
彭于晏Yan11 小时前
Springboot实现数据脱敏
java·spring boot·后端
super_lzb11 小时前
springboot打war包时将外部配置文件打入到war包内
java·spring boot·后端·maven
钱多多_qdd12 小时前
springboot注解(二)
java·spring boot·后端