SpringBoot內(nèi)容協(xié)商快速入門
1.什么內(nèi)容協(xié)商
簡單說就是服務(wù)提供方根據(jù)客戶端所支持的格式來返回對(duì)應(yīng)的報(bào)文,在 Spring 中,REST API 基本上都是以 json 格式進(jìn)行返回,而如果需要一個(gè)接口即支持 json,又支持其他格式,開發(fā)和維護(hù)多套代碼顯然是不合理的,而 Spring 又恰好提供了該功能,那便是ContentNegotiation 在 Spring 中,決定一個(gè)數(shù)據(jù)是以 jso還是xml 分別如下:
favorPathExtension 后綴模式,例如:xxx.json,xxx.xml
favorParameter format模式,例如:xxx?format=json,xxx?format=xml,
通過請(qǐng)求的 Accept 來決定返回的值
2.代碼工程
實(shí)驗(yàn)?zāi)繕?biāo):根據(jù)請(qǐng)求參數(shù)不一樣自動(dòng)切換不同的格式的返回結(jié)果
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>springboot-demo</artifactId> <groupId>com.et</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>ContentNegotiation</artifactId> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-autoconfigure</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>com.fasterxml.jackson.dataformat</groupId> <artifactId>jackson-dataformat-xml</artifactId> </dependency> </dependencies> </project>
controller
package com.et.contentnegotiation.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import java.util.HashMap; import java.util.Map; @Controller public class HelloWorldController { @RequestMapping("/hello") @ResponseBody public Map<String, Object> showHelloWorld(){ Map<String, Object> map = new HashMap<>(); map.put("msg", "HelloWorld"); return map; } }
DemoApplication.java
package com.et.contentnegotiation; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
application.yaml
server: port: 8088 spring: mvc: contentnegotiation: #favor-path-extension: true # header accept favor-parameter: true # url ?format=xml or format=json media-types: json: application/json
以上只是一些關(guān)鍵代碼,所有代碼請(qǐng)參見下面代碼倉庫
代碼倉庫
3.測試
favorParameter 方式
設(shè)置配置文件里面參數(shù)
spring.mvc.contentnegotiation.favor-parameter=true
啟動(dòng)springboot應(yīng)用,
http://127.0.0.1:8088/hello?format=xml http://127.0.0.1:8088/hello?format=json
返回不同格式的結(jié)果
請(qǐng)求的 Accept 來決定返回的值
設(shè)置配置文件里面參數(shù)
spring.mvc.contentnegotiation.favor-path-extension=true
設(shè)置header里面Accept:application/xml 或者application/json
4.引用
到此這篇關(guān)于SpringBoot內(nèi)容協(xié)商快速入門的文章就介紹到這了,更多相關(guān)SpringBoot 內(nèi)容協(xié)商入門內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
eclipse創(chuàng)建java項(xiàng)目并運(yùn)行的詳細(xì)教程講解
eclipse是java開發(fā)的ide工具,是大部分java開發(fā)人員的首選開發(fā)工具,可是對(duì)于一些新Java人員來說,不清楚eclipse怎么運(yùn)行項(xiàng)目?這篇文章主要給大家介紹了關(guān)于eclipse創(chuàng)建java項(xiàng)目并運(yùn)行的相關(guān)資料,需要的朋友可以參考下2023-04-04Java使用NIO包實(shí)現(xiàn)Socket通信的實(shí)例代碼
本篇文章主要介紹了Java使用NIO包實(shí)現(xiàn)Socket通信的實(shí)例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-02-02Mybatis攔截器實(shí)現(xiàn)數(shù)據(jù)分表
當(dāng)數(shù)據(jù)量比較多時(shí),放在一個(gè)表中的時(shí)候會(huì)影響查詢效率,本文主要介紹了Mybatis攔截器實(shí)現(xiàn)數(shù)據(jù)分表,文中通過示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-01-01SpringBoot在 POM 中引入本地 JAR 包的方法
在開發(fā) Spring Boot 應(yīng)用程序時(shí),您可能需要使用本地 JAR 包來添加自定義庫或功能,本文將介紹在 Spring Boot 項(xiàng)目的 POM 文件中如何引入本地 JAR 包,感興趣的朋友跟隨小編一起看看吧2023-08-08java獲取兩個(gè)數(shù)組中不同數(shù)據(jù)的方法
這篇文章主要介紹了java獲取兩個(gè)數(shù)組中不同數(shù)據(jù)的方法,實(shí)例分析了java操作數(shù)組的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-03-03JDK8中String的intern()方法實(shí)例詳細(xì)解讀
String字符串在我們?nèi)粘i_發(fā)中最常用的,當(dāng)然還有他的兩個(gè)兄弟StringBuilder和StringBuilder,接下來通過本文給大家介紹JDK8中String的intern()方法詳細(xì)解讀,需要的朋友可以參考下2022-09-09