1. Problem description:
In SprinvMVC's web program, I send Ajax's POST request on the page, and then use @requestBody to receive the parameters in the request body on the server. During the run, I wanted the server to send Ajax request. The browser kept feedbacking 415 Unsupported Media Type or 400 status code, thinking that there was something wrong with Ajax writing. After searching for the information for a long time, I found that there is something missing in the configuration of the spring-mvc.config file. Of course, it is also possible that you are really missing the settings of the Content-Type parameter in Ajax. After analysis, I should have a problem with the configuration of springMVC-config.xml file.
(Note): 400: (Error request) The server does not understand the syntax of the request. 415: (Unsupported media type) The requested format is not supported by the requested page.
2. Solution:
In the springMVC-config.xml file, a StringHttpMessageConverter request information converter is added, and the configuration fragment is as follows:
<!--- StringHttpMessageConverter bean -->< bean id = "stringHttpMessageConverter" class = "org.springframework.http.converter.StringHttpMessageConverter"/> <!-- Start Spring MVC annotation function to complete the mapping of requests and annotation POJOs-->< bean class ="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" > < property name= "messageConverters" > < list> < ref bean= "mappingJacksonHttpMessageConverter" /> <!-- New StringMessageConverter bean--> < ref bean= "stringHttpMessageConverter" /> < ref bean= "jsonHttpMessageConverter" /> < ref bean= "formHttpMessageConverter" /> </ list> </ property></ bean>
3. Introduction to HttpMessageConverter Request Information Converter:
The HttpMessageConverter interface specifies a converter that can convert Http request information and Http response information in format. There are usually the following converters that implement the HttpMessageConverter interface:
ByteArrayHttpMessageConverter: Responsible for reading binary format data and writing binary format data;
StringHttpMessageConverter: Responsible for reading string format data and writing binary format data;
ResourceHttpMessageConverter: Responsible for reading resource files and writing resource file data;
FormHttpMessageConverter: Responsible for reading data submitted by form (the data format that can be read is application/x-www-form-urlencoded, and cannot read multipart/form-data format data); responsible for writing data in application/x-www-from-urlencoded and multipart/form-data formats;
MappingJacksonHttpMessageConverter: Responsible for reading and writing data in json format;
SourceHttpMessageConverter: Responsible for reading and writing data defined by javax.xml.transform.Source in xml;
Jaxb2RootElementHttpMessageConverter: Responsible for reading and writing data in the xml tag format;
AtomFeedHttpMessageConverter: Responsible for reading and writing data in Atom format;
RssChannelHttpMessageConverter: Responsible for reading and writing data in RSS format;
For more information about HttpMessageConverter, please see:
http://docs.spring.io/spring/docs/3.0.x/api/org/springframework/http/converter/HttpMessageConverter.html
4.HttpMessageConverter request information converter execution process:
When the user sends a request, the @Requestbody annotation will read the data in the request body. The default request converter HttpMessageConverter confirms the data format of the request header by obtaining the Content-Type in the request header, thereby adapting the appropriate converter to the request data. For example, contentType:applicatin/json, the converter will be adapted to MappingJacksonHttpMessageConverter. Similarly, the @Responsebody annotation will enable HttpMessageConverter to detect the Accept property in the Header to adapt the response converter.
Summarize:
When using SpringMVC for server data reception, especially when making Ajax requests, pay special attention to the settings of the contentType attribute and accept attribute, and configure the corresponding converter in springmvc-config.xml. When we use SpringMVC to make Ajax requests, some methods use the response.getWriter().print() method. Another better method is to add the @Responsebody annotation to directly return the Map type data, and the converter automatically converts it to JSON data type.