SpringBoot內(nèi)容協(xié)商快速入門
1.什么內(nèi)容協(xié)商
簡單說就是服務(wù)提供方根據(jù)客戶端所支持的格式來返回對應(yīng)的報文,在 Spring 中,REST API 基本上都是以 json 格式進(jìn)行返回,而如果需要一個接口即支持 json,又支持其他格式,開發(fā)和維護(hù)多套代碼顯然是不合理的,而 Spring 又恰好提供了該功能,那便是ContentNegotiation 在 Spring 中,決定一個數(shù)據(jù)是以 jso還是xml 分別如下:
favorPathExtension 后綴模式,例如:xxx.json,xxx.xml
favorParameter format模式,例如:xxx?format=json,xxx?format=xml,
通過請求的 Accept 來決定返回的值
2.代碼工程
實驗?zāi)繕?biāo):根據(jù)請求參數(shù)不一樣自動切換不同的格式的返回結(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)鍵代碼,所有代碼請參見下面代碼倉庫
代碼倉庫
3.測試
favorParameter 方式
設(shè)置配置文件里面參數(shù)
spring.mvc.contentnegotiation.favor-parameter=true
啟動springboot應(yīng)用,
http://127.0.0.1:8088/hello?format=xml http://127.0.0.1:8088/hello?format=json
返回不同格式的結(jié)果
請求的 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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Mybatis報Type interface *.*Mapper is not&
本文主要介紹了Mybatis報Type interface *.*Mapper is not known to the MapperRegis,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-07-07
Spring?Aop+Redis實現(xiàn)優(yōu)雅記錄接口調(diào)用情況
通常情況下,開發(fā)完一個接口,無論是在測試階段還是生產(chǎn)上線,我們都需要對接口的執(zhí)行情況做一個監(jiān)控,所以本文為大家整理了Spring統(tǒng)計接口調(diào)用的多種方法,希望對大家有所幫助2023-06-06
Java Socket編程實例(三)- TCP服務(wù)端線程池
這篇文章主要講解Java Socket編程中TCP服務(wù)端線程池的實例,希望能給大家做一個參考。2016-06-06
Java結(jié)構(gòu)型設(shè)計模式之裝飾模式詳解
裝飾模式(Decorator Pattern)允許向一個現(xiàn)有的對象添加新的功能,同時又不改變其結(jié)構(gòu)。這種類型的設(shè)計模式屬于結(jié)構(gòu)型模式,它是作為現(xiàn)有類的一個包裝。這種模式創(chuàng)建了一個裝飾類,用來包裝原有的類,并在保持類方法簽名完整性的前提下,提供了額外的功能2023-03-03
SpringBoot實現(xiàn)HTTPS加密通信的詳細(xì)指南
這篇文章主要為大家詳細(xì)介紹了Spring?Boot中HTTPS的實現(xiàn)方案與實戰(zhàn)避坑指南,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2025-06-06
SpringBoot使用thymeleaf實現(xiàn)一個前端表格方法詳解
Thymeleaf是一個現(xiàn)代的服務(wù)器端 Java 模板引擎,適用于 Web 和獨(dú)立環(huán)境。Thymeleaf 的主要目標(biāo)是為您的開發(fā)工作流程帶來優(yōu)雅的自然模板,本文就來用它實現(xiàn)一個前端表格,感興趣的可以了解一下2022-10-10

