SpringBoot內(nèi)容協(xié)商快速入門(mén)
1.什么內(nèi)容協(xié)商
簡(jiǎn)單說(shuō)就是服務(wù)提供方根據(jù)客戶(hù)端所支持的格式來(lái)返回對(duì)應(yīng)的報(bào)文,在 Spring 中,REST API 基本上都是以 json 格式進(jìn)行返回,而如果需要一個(gè)接口即支持 json,又支持其他格式,開(kāi)發(fā)和維護(hù)多套代碼顯然是不合理的,而 Spring 又恰好提供了該功能,那便是ContentNegotiation 在 Spring 中,決定一個(gè)數(shù)據(jù)是以 jso還是xml 分別如下:
favorPathExtension 后綴模式,例如:xxx.json,xxx.xml
favorParameter format模式,例如:xxx?format=json,xxx?format=xml,
通過(guò)請(qǐng)求的 Accept 來(lái)決定返回的值
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)參見(jiàn)下面代碼倉(cāng)庫(kù)
代碼倉(cāng)庫(kù)
3.測(cè)試
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 來(lái)決定返回的值
設(shè)置配置文件里面參數(shù)
spring.mvc.contentnegotiation.favor-path-extension=true
設(shè)置header里面Accept:application/xml 或者application/json

4.引用
到此這篇關(guān)于SpringBoot內(nèi)容協(xié)商快速入門(mén)的文章就介紹到這了,更多相關(guān)SpringBoot 內(nèi)容協(xié)商入門(mén)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Mybatis報(bào)Type interface *.*Mapper is not&
本文主要介紹了Mybatis報(bào)Type interface *.*Mapper is not known to the MapperRegis,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-07-07
Spring?Aop+Redis實(shí)現(xiàn)優(yōu)雅記錄接口調(diào)用情況
通常情況下,開(kāi)發(fā)完一個(gè)接口,無(wú)論是在測(cè)試階段還是生產(chǎn)上線(xiàn),我們都需要對(duì)接口的執(zhí)行情況做一個(gè)監(jiān)控,所以本文為大家整理了Spring統(tǒng)計(jì)接口調(diào)用的多種方法,希望對(duì)大家有所幫助2023-06-06
Java Socket編程實(shí)例(三)- TCP服務(wù)端線(xiàn)程池
這篇文章主要講解Java Socket編程中TCP服務(wù)端線(xiàn)程池的實(shí)例,希望能給大家做一個(gè)參考。2016-06-06
Java結(jié)構(gòu)型設(shè)計(jì)模式之裝飾模式詳解
裝飾模式(Decorator Pattern)允許向一個(gè)現(xiàn)有的對(duì)象添加新的功能,同時(shí)又不改變其結(jié)構(gòu)。這種類(lèi)型的設(shè)計(jì)模式屬于結(jié)構(gòu)型模式,它是作為現(xiàn)有類(lèi)的一個(gè)包裝。這種模式創(chuàng)建了一個(gè)裝飾類(lèi),用來(lái)包裝原有的類(lèi),并在保持類(lèi)方法簽名完整性的前提下,提供了額外的功能2023-03-03
SpringBoot實(shí)現(xiàn)HTTPS加密通信的詳細(xì)指南
這篇文章主要為大家詳細(xì)介紹了Spring?Boot中HTTPS的實(shí)現(xiàn)方案與實(shí)戰(zhàn)避坑指南,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2025-06-06
SpringBoot使用thymeleaf實(shí)現(xiàn)一個(gè)前端表格方法詳解
Thymeleaf是一個(gè)現(xiàn)代的服務(wù)器端 Java 模板引擎,適用于 Web 和獨(dú)立環(huán)境。Thymeleaf 的主要目標(biāo)是為您的開(kāi)發(fā)工作流程帶來(lái)優(yōu)雅的自然模板,本文就來(lái)用它實(shí)現(xiàn)一個(gè)前端表格,感興趣的可以了解一下2022-10-10

