Foreword: Recently, when writing a Java Web background, I adopted the Spring+mybatis+mysql method. Record the key issues that are encountered
interface returns data-relatedreturn null after using @responsebody
Description: just run the background, happy to test the interface data, the result is to return null anyway ,finally through a variety of Baidu, found that the original is not introduced the key jar package.
Workaround: The Jackson jar package (Jackson Core and Jackson Mapper) needs to be introduced into the
Figure:
use @requestmapping to return Chinese garbled charactersCause Analysis: (Basic online is the same answer)
First of all: It is determined (after many tests) that the Chinese characters are garbled only if the return value is String, and when the return value is map<string, object>, or other type, there is no Chinese garbled appearance.
and then find the reason: the reason is that this is one of spring MVC bug,spring MVC has a series of httpmessageconverter to deal with the return value of @responsebody annotations, If you return to list or otherwise use Mappingjacksonhttpmessageconverter and return string, use Stringhttpmessageconverter, This convert uses a character set that is iso-8859-1 and final. Therefore, garbled characters will appear when there is Chinese in the return JSON.
[Java]View PlainCopy
- Public static final Charset Default_charset = Charset.forname ("iso-8859-1");
solution (and whether to try it successfully): tried many kinds of online methods, some simply useless, there are some when the client's accep is Application/json; Finally, it is a combination of client-side modifications to successfully solve the problem.
Environment: SPRINGMVC 3.1
the client is divided into three different types of requests:1. Direct get access in the browser, accept is "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"2. With jquery's JSONP Ajax request, accept is "*/*"3. With MUI Ajax request under h5+ environment, accept is "Application/json;charset=utf-8"(ps:h5+ is a recently done cross-platform mobile project development environment, in addition to have tried the default accept "Application/json" words regardless of the server side with which method, in the configuration, will return garbled, so finally had to manually add Charset=utf-8.)
Note: If nothing, the first and the second is the return of garbled, the third due to the manual return format, so there is no garbled, here is the third group to do the comparison because there are some methods will cause the third group can not access the normal. Try Method One: Add the following code to the Mvc:annotation-driven in the configuration file
[HTML]View PlainCopy
- <mvc:annotation-driven >
- <!--message Converters --
- <mvc:message-converters register-defaults="true">
- <Bean class="Org.springframework.http.converter.StringHttpMessageConverter">
- <property name= "supportedmediatypes" value="text/html;charset=utf-8"/>
- </Bean>
- </mvc:message-converters>
- </mvc:annotation-driven>
principle: Stringhttpmessageconverter has a list<mediatype> supportedmediatypes attribute in the parent class to store The Stringhttpmessageconverter supports mediatype types that require special handling, with the default character set if the mediatype type to be processed is not in the Supportedmediatypes list.
Final Result:Request Method 1 garbled language Request Method 2 Returns the correct Chinese Request Method 3 Returns the correct ChineseTry Method Two: Add the following code to the Mvc:annotation-driven in the configuration file
[HTML]View PlainCopy
- <mvc:annotation-driven>
- <mvc:message-converters>
- <Bean class="Org.springframework.http.converter.StringHttpMessageConverter">
- <property name="Supportedmediatypes">
- <list>
- <span style="White-space:pre"> </span> <value> text/html; charset=UTF-8</value>
- <value>application/json; charset=UTF-8</value>
- <value>*/*; charset=UTF-8</value>
- </list>
- </Property>
- </Bean>
- </mvc:message-converters>
- </mvc:annotation-driven>
principle: The principle above, but here add a few value.
Final Result:Request Method 1 garbled languageRequest Method 2 Returns the correct ChineseRequest Method 3 Returns the correct ChineseThe reason why this type of method can not be solved correctly, the reasons are different on the Internet, it is not easy to make a conclusion. In a word, this approach does not solve the problem at the moment.
Try method Three: Configuration in @requestmapping produces= "TEXT/HTML;CHARSET=UTF-8;"
[Java]View PlainCopy
- @RequestMapping (value = "* * *", produces="TEXT/HTML;CHARSET=UTF-8;")
principle: manually to the corresponding accept return to develop format encoding data.
Final Result:Request Method 1returns the correct ChineseRequest Method 2 Returns the correct Chinese request Method 3 cannot request, out of the wrong, because produces did not add application/json; corresponding head.
Try method Four: Concurrent configuration in @requestmapping produces={"Application/json;", "Text/html;charset=utf-8;"}
[Java]View PlainCopy
- @RequestMapping (value = "* * *", produces={"Application/json;","Text/html;charset=utf-8;"})
principle: manually to the corresponding accept return to develop format encoding data.
Final Result:Request Method 1 Return Chinese garbled request Method 2 return Chinese garbled request Method 3returns the correct Chinese
Try method Five: Concurrent configuration in @requestmapping produces={"text/html;charset=utf-8;", "Application/json;"}
[Java]View PlainCopy
- @RequestMapping (value = "* * *", produces={"text/html;charset=utf-8;","Application/json;"})
Note: The difference between this and the previous method is that the order of the produces inside is reversed.
principle:Manually give the corresponding accept back the format encoding data.
Final Result:Request Method 1returns the correct ChineseRequest Method 2returns the correct ChineseRequest Method 3 Returns the correct Chinese
method Four and method Five contrast analysis:find produces set multiple accept only the first charset is useful,The following accept settings are valid (you cannot receive the corresponding accept request because it is not set), but the CharSet setting is not valid. Requires the client to manually make the CharSet.The exact reason is not clear (forgive me for not being proficient)
so the conclusion is:produces={"TEXT/HTML;CHARSET=UTF-8;", "Application/json;"}This setting, so that the normal browser request can display the Chinese language, and the client's impersonation request (can be Ajax or HTTP) manually specify the charset of accept, you can normally receive Chinese.
Original connection:
Springmvc encountered the pit, returned Chinese garbled and Ajax cross-domain
SPRINGMVC using AJAX requests to return Chinese garbled characters