一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|編程技術(shù)|正則表達式|C/C++|IOS|C#|Swift|Android|JavaScript|易語言|

服務(wù)器之家 - 編程語言 - JAVA教程 - spring接口通過配置支持返回多種格式(xml,json,html,excel)

spring接口通過配置支持返回多種格式(xml,json,html,excel)

2021-03-07 12:15Jekyll JAVA教程

這篇文章主要給大家介紹了關(guān)于spring接口如何通過配置支持返回多種格式(xml,json,html,excel)的相關(guān)資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。

1. 簡介

本文主要給大家介紹使用SpringMVC的后端服務(wù)如何通過配置來支持多種返回值類型(xml,json,html,excel)

這里的代碼使用的是springboot,下載地址:https://github.com/xiagn825/springboot-todolist/tree/springboot-ContentNegotiation

2. 基礎(chǔ)概念

2.1 HttpHeader中Content-Type和Accept設(shè)置的區(qū)別

Accept:接口要返回給客戶端的數(shù)據(jù)格式

?
1
curl --header 'Accept:application/json' http://localhost:8080/todo

Content-Type:客戶端發(fā)送給服務(wù)器端的數(shù)據(jù)格式

?
1
curl -X PUT --header 'Content-Type:application/json' -d '{"title":"周末日程","content":"睡覺"}' http://localhost:8080/todo

2.2 SpringMVC生成輸出的兩種方式

1) 當(dāng)服務(wù)端使用Restful的方式,只為客戶端的ajax或其他服務(wù)端請求提供數(shù)據(jù)時,通常會使用@ResponseBody來標(biāo)識你的返回,這時候Spring使用HttpMessageConverter來把返回的對象格式化成所需的格式。

2) 當(dāng)你需要提供表現(xiàn)層(比如:HTML),這時候SpringMVC使用ViewResolver來將處理你的返回。

有時候你的應(yīng)用程序這兩者都要提供

2.3 SpringMVC輸出格式判定

很多時候為了支持多個系統(tǒng)或多個終端,你需要讓相同的數(shù)據(jù)已不同的表現(xiàn)形式輸出。

SpringMVC使用ContentNegotationStrategy來判定用戶請求希望得到什么格式的數(shù)據(jù)。

ContentNegotationStrategy通過三種方式來識別用戶想要返回什么樣的數(shù)據(jù)

  • 通過請求URL后綴:http://myserver/myapp/accounts/list.html 返回html格式
  • 通過請求的參數(shù):http://myserver/myapp/accounts/list?format=xls 該設(shè)置默認不開啟,默認key是format。
  • 通過HTTP Header的Accept:Accept:application/xml 優(yōu)先級由上至下

請看如下配置

?
1
2
3
4
5
6
7
8
9
10
@Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
 configurer.favorPathExtension(false)
   .favorParameter(true)
   .parameterName("mediaType")
   .defaultContentType(MediaType.APPLICATION_JSON)
   .mediaType("xml", MediaType.APPLICATION_XML)
   .mediaType("html", MediaType.TEXT_HTML)
   .mediaType("json", MediaType.APPLICATION_JSON);
}

在你工程的WebMvcConfig中加入以上配置,表示關(guān)閉URL后綴的規(guī)則,打開請求參數(shù)規(guī)則并設(shè)置請求參數(shù)為'mediaType',默認的返回格式是json,還支持返回xml,html。

這三個組件是用來處理返回不同格式輸出的關(guān)鍵

  • Request Mappings: 決定不同的請求到不同的方法并返回不同的格式.
  • View Resolution: 根據(jù)類型返回合適的表示層.
  • HttpMessageConverters: 將request中的參數(shù)轉(zhuǎn)換成java對象,將java對象轉(zhuǎn)換成相應(yīng)的輸出格式到response.

2.4 RequestMappings

2.4.1 RequestMappingHandlerMapping

我們在spring中通常使用的就是RequestMappingHandlerMapping,根據(jù)RequestMappingInfo,細化匹配條件,整體的查找過程如下:

AbstractHandlerMethodMapping實現(xiàn)接口getHandlerInternal

  1. 使用UrlPathHelper查找request對應(yīng)的path

  2. 查找path對應(yīng)的HandlerMethod

    2.1 從urlMap中直接等值匹配查找匹配條件RequestMappingInfo

    2.2 如果等值查找到匹配條件,將其添加到match條件中

    2.3 如果沒有找到匹配條件,使用所有的handlerMethod的RequestMappingInfo進行匹配

    2.4 對匹配到的Match進行排序,取出最高優(yōu)先級的Match,并核對是否是唯一的最高優(yōu)先級

    2.5 對匹配到條件,沒有匹配到條件的兩種情況,分別進行封裝

  3. 封裝HandlerMethod,確保bean中存的是實例    ContentNegotiationManager其中提供了針對miniType的match條件比較,使框架可以匹配到最合適的處理方法。

2.5 HttpMessageConverter

2.5.1 The Default Message Converters

SpringMvc默認會加載下列HttpMessageConverters:

?
1
2
3
4
5
6
7
8
9
10
ByteArrayHttpMessageConverter – converts byte arrays
StringHttpMessageConverter – converts Strings
ResourceHttpMessageConverter – converts org.springframework.core.io.Resource for any type of octet stream
SourceHttpMessageConverter – converts javax.xml.transform.Source
FormHttpMessageConverter – converts form data to/from a MultiValueMap<String, String>.
Jaxb2RootElementHttpMessageConverter – converts Java objects to/from XML (added only if JAXB2 is present on the classpath)
MappingJackson2HttpMessageConverter – converts JSON (added only if Jackson 2 is present on the classpath)
MappingJacksonHttpMessageConverter – converts JSON (added only if Jackson is present on the classpath)
AtomFeedHttpMessageConverter – converts Atom feeds (added only if Rome is present on the classpath)
RssChannelHttpMessageConverter – converts RSS feeds (added only if Rome is present on the classpath)

我們?nèi)绻祷氐氖鞘褂聾ResponseBody來標(biāo)識的,那么框架會使用HttpMessageConverter來處理返回值,默認的xmlCoverter不是特別好用,依賴返回實體對象上的@XmlRootElement注解,不是很方便所以引入輔助類庫,并自定義MessageConverter這樣可以直接將返回的對象處理成xml格式。

Gradle import library

?
1
2
compile group: 'org.springframework', name: 'spring-oxm', version: '4.3.9.RELEASE'
compile group: 'com.thoughtworks.xstream', name: 'xstream', version: '1.4.10'

configuration

?
1
2
3
4
5
6
7
8
9
10
11
12
13
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
 converters.add(createXmlHttpMessageConverter());
 super.configureMessageConverters(converters);
}
private HttpMessageConverter<Object> createXmlHttpMessageConverter() {
 MarshallingHttpMessageConverter xmlConverter =
   new MarshallingHttpMessageConverter();
 XStreamMarshaller xstreamMarshaller = new XStreamMarshaller();
 xmlConverter.setMarshaller(xstreamMarshaller);
 xmlConverter.setUnmarshaller(xstreamMarshaller);
 return xmlConverter;
}

2.6 View Resolution

2.6.1 頁面render(freemarker)

當(dāng)需要返回頁面時就需要由合適的viewResolver來繪制畫面,這里采用freemarker作為頁面引擎。

Gradle import library

?
1
compile("org.springframework.boot:spring-boot-starter-freemarker")

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,如果有疑問大家可以留言交流,謝謝大家對服務(wù)器之家的支持。

原文鏈接:http://xiagn825.github.io/2017/06/23/blog/

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 九九精品国产亚洲A片无码 九九99热久久999精品 | 久久久无码精品亚洲A片软件 | 国产精品免费_区二区三区观看 | 亚洲狼人综合干 | fc2免费人成为视频 eeuss18影院www国产 | 毛片在线免费视频 | 果冻传媒mv在线观看入口免费 | 午夜精品久久久久 | 国产成人盗摄精品 | 免费在线观看中文字幕 | 97视频久久久 | 成人综合网站 | 日本在线视频免费观看 | 男同精品视频免费观看网站 | 日本狠狠操 | 青草热视频 | 国产福利资源 | 国产美女亚洲精品久久久综合 | 思思久久精品在热线热 | 欧美18-19sex性处 | 日韩毛片大全免费高清 | 色综合久久九月婷婷色综合 | 欧美男女爱爱视频 | 4p高h三男一女 | 欧美男同videos | 国产haodiaose最新 | 亚洲第一成年免费网站 | aaa毛片手机在线现看 | 不卡视频一区二区 | 女教师雪白老汉 | 国产精品久久久久无毒 | 亚洲欧洲淘宝天堂日本 | 久久强奷乱码老熟女 | 成年美女黄网色大观看全 | 亚洲香蕉网久久综合影院3p | 桃色公寓 | 日本 片 成人 在线 日b视频免费 | 欧美黑人成人免费全部 | 色综合久久天天综合 | 四虎影视4hu最新地址在线884 | 国产播放啪视频免费视频 |