1. 簡介
本文主要給大家介紹使用SpringMVC的後端服務如何通過配置來支持多種返回值類型(xml,json,html,excel)
這裡的代碼使用的是springboot,下載地址:https://github.com/xiagn825/springboot-todolist/tree/springboot-ContentNegotiation
2. 基礎概念
2.1 HttpHeader中Content-Type和Accept設置的區別
Accept:接口要返回給客戶端的數據格式
curl --header 'Accept:application/json' http://localhost:8080/todo
Content-Type:客戶端發送給服務器端的數據格式
curl -X PUT --header 'Content-Type:application/json' -d '{"title":"週末日程","content":"睡覺"}' http://localhost:8080/todo2.2 SpringMVC生成輸出的兩種方式
1) 當服務端使用Restful的方式,只為客戶端的ajax或其他服務端請求提供數據時,通常會使用@ResponseBody來標識你的返回,這時候Spring使用HttpMessageConverter來把返回的對象格式化成所需的格式。
2) 當你需要提供表現層(比如:HTML),這時候SpringMVC使用ViewResolver來將處理你的返回。
有時候你的應用程序這兩者都要提供
2.3 SpringMVC輸出格式判定
很多時候為了支持多個系統或多個終端,你需要讓相同的數據已不同的表現形式輸出。
SpringMVC使用ContentNegotationStrategy來判定用戶請求希望得到什麼格式的數據。
ContentNegotationStrategy通過三種方式來識別用戶想要返回什麼樣的數據
請看如下配置
@Overridepublic 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中加入以上配置,表示關閉URL後綴的規則,打開請求參數規則並設置請求參數為'mediaType',默認的返回格式是json,還支持返回xml,html。
這三個組件是用來處理返回不同格式輸出的關鍵
2.4 RequestMappings
2.4.1 RequestMappingHandlerMapping
我們在spring中通常使用的就是RequestMappingHandlerMapping,根據RequestMappingInfo,細化匹配條件,整體的查找過程如下:
AbstractHandlerMethodMapping實現接口getHandlerInternal
1. 使用UrlPathHelper查找request對應的path
2. 查找path對應的HandlerMethod
2.1 從urlMap中直接等值匹配查找匹配條件RequestMappingInfo
2.2 如果等值查找到匹配條件,將其添加到match條件中
2.3 如果沒有找到匹配條件,使用所有的handlerMethod的RequestMappingInfo進行匹配
2.4 對匹配到的Match進行排序,取出最高優先級的Match,並核對是否是唯一的最高優先級
2.5 對匹配到條件,沒有匹配到條件的兩種情況,分別進行封裝
3. 封裝HandlerMethod,確保bean中存的是實例ContentNegotiationManager其中提供了針對miniType的match條件比較,使框架可以匹配到最合適的處理方法。
2.5 HttpMessageConverter
2.5.1 The Default Message Converters
SpringMvc默認會加載下列HttpMessageConverters:
ByteArrayHttpMessageConverter converts byte arraysStringHttpMessageConverter converts StringsResourceHttpMessageConverter converts org.springframework.core.io.Resource for any type of octet streamSourceHttpMessageConverter converts javax.xml.transform.SourceFormHttpMessageConverter 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)
我們如果返回的是使用@ResponseBody來標識的,那麼框架會使用HttpMessageConverter來處理返回值,默認的xmlCoverter不是特別好用,依賴返回實體對像上的@XmlRootElement註解,不是很方便所以引入輔助類庫,並自定義MessageConverter這樣可以直接將返回的對象處理成xml格式。
Gradle import library
compile group: 'org.springframework', name: 'spring-oxm', version: '4.3.9.RELEASE'compile group: 'com.thoughtworks.xstream', name: 'xstream', version: '1.4.10'
configuration
@Overridepublic 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)
當需要返回頁面時就需要由合適的viewResolver來繪製畫面,這裡採用freemarker作為頁面引擎。
Gradle import library
compile("org.springframework.boot:spring-boot-starter-freemarker")總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對武林網的支持。