The spring interface supports the return of multiple formats (XML, JSON, HTML, Excel) through configuration

1. Introduction

This article mainly introduces how the back-end service using spring MVC supports multiple return value types (XML, JSON, HTML, Excel) through configuration

The code here uses springboot. Download address: https://github.com/xiagn825/springboot-todolist/tree/springboot-ContentNegotiation

2. Basic concepts

2.1 difference between content type and accept settings in httpheader

Accept: the data format to be returned to the client by the interface

Content type: the data format sent by the client to the server

2.2 two methods for spring MVC to generate output

1) When the server uses the restful method to only provide data for the client's Ajax or other server requests, it usually uses @ ResponseBody to identify your return. At this time, spring uses httpmessageconverter to format the returned object into the required format.

2) When you need to provide a presentation layer (such as HTML), spring MVC uses viewresolver to process your return.

Sometimes your application provides both

2.3 spring MVC output format determination

Many times, in order to support multiple systems or multiple terminals, you need to output the same data in different forms.

Spring MVC uses the content negotiation strategy to determine what format of data a user requests.

Content negotiation strategy identifies what data users want to return in three ways

See the following configuration

Add the above configuration to the webmvcconfig of your project, which means to close the rule of URL suffix, open the request parameter rule and set the request parameter to 'mediatype'. The default return format is JSON, and it also supports the return of XML and HTML.

These three components are the key to handling the return of output in different formats

2.4 RequestMappings

2.4. 1 RequestMappingHandlerMapping

We usually use requestmappinghandlermapping in spring. According to requestmappinginfo, we refine the matching conditions. The overall search process is as follows:

Abstracthandlermethodmapping implements the interface gethandlerinternal

  1. Use the urlpathhelper to find the path corresponding to the request

  2. Find the handlermethod corresponding to path

2.1 find the matching condition requestmappinginfo by direct equivalent matching from urlmap

2.2 if the equivalence finds a matching condition, add it to the match condition

2.3 if no matching condition is found, use the requestmappinginfo of all handlermethods for matching

2.4 sort the matched matches, take out the match with the highest priority, and check whether it is the only highest priority

2.5 package the two cases that match the condition and do not match the condition

  3. Encapsulate the handlermethod to ensure that the instance contentnegotiation manager is stored in the bean, which provides a match condition comparison for minitype, so that the framework can match the most appropriate processing method.

2.5 HttpMessageConverter

2.5. 1 The Default Message Converters

Spring MVC loads the following httpmessageconverters by default:

If the returned object is identified by @ ResponseBody, the framework will use httpmessageconverter to process the returned value. The default xmlconverter is not particularly easy to use and depends on the @ xmlrootelement annotation on the returned entity object, which is not very convenient. Therefore, the auxiliary class library is introduced and the messageconverter is customized, so the returned object can be directly processed into XML format.

Gradle import library

configuration

2.6 View Resolution

2.6. 1 page render (freemaker)

When you need to return to the page, you need to draw the picture by an appropriate viewresolver. Here freemaker is used as the page engine.

Gradle import library

summary

The above is the whole content of this article. I hope the content of this article has a certain reference value for your study or work. If you have any questions, you can leave a message. Thank you for your support for programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>