[Spring MVC] @ ResponseBody returned result garbled Problem Solution
Problems Found
Add @ ResponseBody to the Controller class method and return a string directly. The result is garbled.
As follows:
MockHttpServletResponse:
Status = 200
Error message = null
Headers = {Content-Type = [text/plain; charset = ISO-8859-1], Content-Length = [45]}
Content type = text/plain; charset = ISO-8859-1
Body = {
"Msg ":"??????????????? NameEntTypeCode ?? "
}
Access via a browser is also garbled.
Analyze problem 1. Analyze logs
Set the log level to DEBUG and view the log. The excerpt is as follows:
(1)
"Msg": "The required parameter" nameEntTypeCode "is not input when the rule is called ". "
}]>
This is still true. The Handle call is complete, and the returned value is obtained. The next step is View.
(2)
ModelAndViewMethodReturnValueHandler @ 12eae9c] supports [class java. lang. String]>
Web. method. annotation. ModelMethodProcessor @ 12c50a4] supports [class java. lang. String]>
Web. servlet. mvc. method. annotation. ViewMethodReturnValueHandler @ 56a5b] supports [class java. lang. String]>
Web. servlet. mvc. method. annotation. HttpEntityMethodProcessor @ 9463a0] supports [class java. lang. String]>
Web. method. annotation. ModelAttributeMethodProcessor @ 1aad6f4] supports [class java. lang. String]>
Web. servlet. mvc. method. annotation. RequestResponseBodyMethodProcessor @ 1c45fa4] supports [class java. lang. String]>
Find the culprit.
Accept. PathExtensionContentNegotiationStrategy $ JafMediaTypeFactory]
Loading Java Activation Framework FileTypeMap from class path resource
[Org/springframework/mail/javamail/mime. types] // The default supported types are read from here
Mvc. method. annotation. AbstractMessageConverterMethodProcessor "Msg": "The required parameter" nameEntTypeCode "is not input when the rule is called ". "
}] As "text/plain; charset = ISO-8859-1" using [org. springframework. http. converter. StringHttpMessageConverter @ 1efa83c]>
Find the culprit. How did he commit the crime?
I guess it is probably StringHttpMessageConverter that injects AbstractMessageConverterMethodProcessor.
"Text/plain; charset = ISO-8859-1" is the property of StringHttpMessageConverter.
2. Try to solve the problem
(1) AbstractMessageConverterMethodProcessor is an abstract class. You can find a solution on StringHttpMessageConverter.
(2)
If org. springframework. http. converter. StringHttpMessageConverter is a Singleton, you can configure it yourself.
Class = "org. springframework. http. converter. StringHttpMessageConverter">
Text/plain; charset = UTF-8
In -The configuration in servlet. xml is invalid. Confirm to put Before.
(3) injection is incorrect. Check the source code of Spring.
3. Search for injection ports
(1)
In the RequestMappingHandlerAdapter constructor:
Public RequestMappingHandlerAdapter (){
StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter ();
...
This. messageConverters. add (stringHttpMessageConverter );
}
It's so rude to be new.
This is only the default action and should be covered.
(2)
Followed by code, followed
Private List GetDefaultReturnValueHandlers (){
List Handlers = new ArrayList ();
// Single-purpose return value types
Handlers. add (new HttpEntityMethodProcessor (getMessageConverters (), this. contentNegotiationManager ));
// Annotation-based return value types
Handlers. add (new RequestResponseBodyMethodProcessor (getMessageConverters (), this. contentNegotiationManager ));
}
Then find:
Public void setMessageConverters (List > MessageConverters ){
This. messageConverters = messageConverters;
}
(3)
Try:
Text/plain; charset = UTF-8
This is the error of the old version:
Cvc-complex-type.2.1: Element 'mvc: annotation-driven' must not contain characters or element Information items [sublevel] Because the content type of this type is empty.
See the xsd version in the mvc namespace.
Okay, here it is: http://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
(4)
Fixed, okay:
MockHttpServletResponse:
Status = 200
Error message = null
Headers = {Content-Type = [text/plain; charset = UTF-8], Content-Length = [79]}
Content type = text/plain; charset = UTF-8
Body = {
"Msg": "The required parameter" nameEntTypeCode "is not input when the rule is called ". "
}
Forwarded URL = null
Redirected URL = null
Cookies = []
Summary
1. When you encounter problems, read more logs and read more code.
2. There are many injection ports for Spring MVC, which is too cool, but it should be used with caution to consider the impact scope.
3. HttpMessageConverter is an artifact that converts input parameters and return values. You can use a custom Media Type to achieve amazing results.