Discover problems
The Controller class method is added @responsebody, directly returns the string, the result is garbled.
As shown below:
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?? "
}
Browser access is also garbled.
Analysis of issue 1, analysis log
Log level set to debug, view log, excerpt as follows:
(1)
<method [Handleruleexception] returned [{
"MSG": "The required parameter was not passed in when the rule was called:" Nameenttypecode ". "
}]>
It's still right to be here. The handle call is completed, the return value is obtained, and then the view is returned.
(2)
[email protected]] Supports [class java.lang.string]>
[email protected]] Supports [class java.lang.string]>
[email protected]6A5B] supports [class java.lang.string]>
[email protected]A0] supports [Class java.lang.string]>
[email protected]] Supports [class java.lang.string]>
Web.s[email protected]1C45FA4] supports [class java.lang.string]>
Find the culprit now.
Accept. Pathextensioncontentnegotiationstrategy$jafmediatypefactory]
Loading Java Activation Framework Filetypemap from class path resource
[Org/springframework/mail/javamail/mime.types]//The default supported type is read from here
Mvc.method.annotation.abstractmessageconvertermethodprocessor<written [{
"MSG": "The required parameter was not passed in when the rule was called:" Nameenttypecode ". "
}] as "text/plain;charset=iso-8859-1" using [[Email protected]fa83c]>
Find the culprit. How did he do it?
I guess the stringhttpmessageconverter was injected into the abstractmessageconvertermethodprocessor.
"Text/plain;charset=iso-8859-1" is a property of Stringhttpmessageconverter.
2. Try to solve
(1) Abstractmessageconvertermethodprocessor is an abstract class, find a way on stringhttpmessageconverter.
(2)
If Org.springframework.http.converter.StringHttpMessageConverter is a singleton, you can configure it yourself to see
<bean id= "Stringhttpmessageconverter"
class= "Org.springframework.http.converter.StringHttpMessageConverter" >
<property name= "Supportedmediatypes" >
<list>
<value>text/plain;charset=UTF-8</value>
</list>
</property>
</bean>
The configuration is not valid in <xxx>-servlet.xml. The confirmation was placed before <mvc:annotation-driven>.
(3) Injection posture is not right, or honestly look at the spring source code bar
3. Find the injection port
(1)
In the Requestmappinghandleradapter constructor:
Public Requestmappinghandleradapter () {
Stringhttpmessageconverter stringhttpmessageconverter = new Stringhttpmessageconverter ();
...
This.messageConverters.add (Stringhttpmessageconverter);
}
It's just so new, it's so rough.
This is only the default behavior and there should be an override method.
(2)
With the code, follow the
Private Listlist
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 (listThis.messageconverters = messageconverters;
}
(3)
Try it:
<mvc:annotation-driven>
<mvc:message-converters>
<bean class= " Org.springframework.http.converter.StringHttpMessageConverter,
<property name= "Supportedmediatypes";
<list>
<value>text/plain;charset=utf-8</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc: Annotation-driven>
This is the old version of the error:
cvc-complex-type.2.1: Element ' Mvc:annotation-driven ' must contain no characters or element information items [ Child], because the content type of the type is empty. The
looks at the XSD version in the MVC namespace.
OK, here it is: http://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
(4)
Fix, OK:
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 was not passed in when the rule was called:" Nameenttypecode ". "
}
Forwarded URL = null
redirected URL = null
Cookies = []
Summarize
1, encounter problems to read more logs, look at the code.
2, Spring MVC inject a lot of, too cool, but should be cautious to use, consider the scope of impact.
3, Httpmessageconverter is an artifact, you can complete the conversion of incoming parameters and return values, through the custom media type can achieve magical effect, the opportunity to study.
[Spring MVC] @ResponseBody the solution to the garbled problem of returning results