Use spring MVC to return to Json,chrome, Firefox is not a problem to locate

Source: Internet
Author: User

Reprinted http://ks.netease.com/blog?id=4024 Li JingScene:The front-end post requests the same URL address, there is a normal return JSON on the Chrome browser, but the Firefox browser is reported 500 error. Here's how the controller layer is written:         other similar interfaces can be used normally, looking at the other interface's wording, the difference is that one more header.  The only difference from other interfaces is that you write a accept=application/jsonless.  What is this for? By looking at the source code and the comparison of the two browser request headers, finally find out the cause of the problem, the following one by one. See the background error:according to the error, it can be seen that the template parsing error, how to use the FF request when using XML to parse it? The app uses Freemarker for page rendering and is configured as follows:
<Bean
>
<Property Name= "Defaultcontenttype" value= "application/JSON "/>
<Property name= "MediaTypes" >
<Map>
<Entry key= "HTML" value= "text/htmlcharset=UTF-8 "/>
<Entry key= "JSON" value= "application/Jsoncharset=UTF-8 "/>
<Entry key= "xml" value= "application/xmlcharset=UTF-8 "/>
</Map>
</Property>
<Property name= "Viewresolvers" >
<List>
<Bean
>
<Property name= "Cache" value= "false"/>
<Property name= "prefix" value= ""/>
<Property name= "suffix" value= ". FTL"/>
<Property Name= "Exposespringmacrohelpers" value= "true"/>
<Property Name= "Exposerequestattributes" value= "true"/>
<Property Name= "Exposesessionattributes" value= "true"/>
<Property Name= "Allowsessionoverride" value= "true"/>
<Property Name= "ContentType" value= "text/htmlcharset=UTF-8 "/>
</Bean>
</List>
</Property>
<Property name= "Defaultviews" >
<List>
<Bean
>
<Property Name= "Objectmapper" ref= "Objectmapper"/>
<Property Name= "ContentType" value= "application/Jsoncharset=UTF-8 "/>
<Property name= "Modelkeys" >
<Set>
<Value>msg</Value>
<Value>res</Value>
<Value>errmsg</Value>
</Set>
</Property>
</Bean>
<Bean id= "Marshallingview"
>
<Property Name= "Marshaller" ref= "Xstreammarshaller" ></Property>
<Property Name= "Modelkey" value= "MSG" ></PROPERTY>
                    <property name= "ContentType"  value= "Application/xml"/
                </BEAN>
             </LIST>
         </PROPERTY>
     </BEAN>
Freemarker has two default return results, JSON and XML, respectively. The result of the final return is handled by the Org.springframework.web.servlet.view.ContentNegotiatingViewResolver class, which determines which view method to use to return. the specific conversion method is as follows:Take a look at the difference between the Chrome request header and the Firefox request header:The value of the accept in Chrome's request header is: */*corresponding to the above steps:1, get the request in the head of the Accept message: */*

2. Obtain the media type that supports accept in the converter: */*

3. Whether the media type supported in the converter is compatible with the request. Accept type: */*

4. Select the best view according to the parameters of the request.

Configuration files configured with Defaltview for Mappingjackson2jsonview and Marshallingview, order matching, for * when directly matched on the first, live selection Mappingjackson2jsonview parsing.

The request header's accept is text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 in Firefoxcorresponding to the above steps:1, get the request in the head of the Accept information: text/html, Application/xhtml+xml, application/xml;q=0.9, */*;q=0.8

2. Obtain the media type that supports accept in the converter: */*

3. Whether the media type supported in the converter is compatible with the requested accept type: text/html, Application/xhtml+xml, application/xml;q=0.9, */*;q=0.8

4. Select the best view according to the parameters of the request.

The configuration files are configured with Defaltview for Mappingjackson2jsonview and Marshallingview, and the sequence matches when matched to Application/xml, and the live return uses Marshallingview parsing.

The above steps are in the controller does not add headers = {"Accept=application/json"}, then why add this code, Firefox can run it?

The result of the above steps is:

1, get the request in the head of the Accept information: text/html, Application/xhtml+xml, application/xml;q=0.9, */*;q=0.8

2. Obtain the media type that supports accept in the converter:application/json;q=0.8

3. Whether the media type supported in the converter is compatible with the request. Accept type: application/json;q=0.8

4, according to the request and other parameters to choose the best view:candidateviews type of Application/json, natural with Mappingjackson2jsonview analysis.

Look at the above steps we already understand, plus accept=application/json after the media type can be directly for the Application/json, so you can directly use Mappingjackson2jsonview parsing, The JSON data format is then returned.

Another experiment, is to change the DEFAULTVEIWS configuration order, put Marshallingview in front, without adding accept=application/json, with chrome request, the result is returned in XML format.

In other words, when accept is */*, he is in order to match.

In spring MVC, if the format you want to return is specified, you can also configure the produces variable:

@RequestMapping(value = "/upload",produces="application/json;charset=UTF-8")

can also achieve the same effect.

Conclusion:

With spring MVC, if the returned data is malformed, you need to look at the accept of the request header, the header configuration of the controller layer, and the rendering type and configuration order of the template in the configuration file, which can be used to locate the reason in these three ways.

Use spring MVC to return to Json,chrome, Firefox is not a problem to locate

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.