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

相关推荐
J_liaty1 天前
SpringBoot + EMQX:打造物联网设备数据双向通讯的完整解决方案
spring boot·物联网·emqx
Coder_Boy_1 天前
基于SpringAI的在线考试系统-考试系统DDD(领域驱动设计)实现步骤详解
java·数据库·人工智能·spring boot
crossaspeed1 天前
Java-SpringBoot的启动流程(八股)
java·spring boot·spring
这儿有个昵称1 天前
互联网大厂Java面试场景:从Spring框架到微服务架构的提问解析
java·spring boot·微服务·kafka·grafana·prometheus·数据库优化
Coder_Boy_2 天前
基于SpringAI的在线考试系统-DDD(领域驱动设计)核心概念及落地架构全总结(含事件驱动协同逻辑)
java·人工智能·spring boot·微服务·架构·事件驱动·领域驱动
小北方城市网2 天前
SpringBoot 集成 RabbitMQ 实战(消息队列解耦与削峰):实现高可靠异步通信
java·spring boot·python·微服务·rabbitmq·java-rabbitmq·数据库架构
程序员老徐2 天前
SpringBoot嵌入Tomcat注册Servlet、Filter流程
spring boot·servlet·tomcat
guslegend2 天前
第1章:快速入门SpringBoot
spring boot
Coder_Boy_2 天前
基于SpringAI的在线考试系统-考试模块前端页面交互设计及优化
java·数据库·人工智能·spring boot
李慕婉学姐2 天前
Springboot旅游景点管理系统2fj40iq6(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
数据库·spring boot·后端