Configuring RESTful style controllers in Springmvc

Source: Internet
Author: User

In general, HTTP requests only need to get and post to meet the project needs, and why also use restful may be to make the request URL look more intuitive, good-looking bar.

How to use RESTful requests: Get,post,put,patch,delete

SPRINGMVC is best for Get and post support by default, like Put,putch,delete, but it only supports the controller to receive URL pass parameters by default, and if we want to pass parameters like post, We need to do some settings in the backend and front end, such as the front-end request to manually set the content type of the request header to JSON, the backend also to match the parameters of the front-end setting controller to receive a request body, rather than the normal form or URL parameters, using requestbody annotations to receive, The SPRINGMVC will use the message processor to help convert the JSON-formatted request body to the Java bean, particularly troublesome, so there are three solutions available:

1. Modify Tomcat's Server.xml:

<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" parseBodyMethods="POST,PUT,DELETE" URIEncoding="UTF-8" />

经过测试这种是可行的,只是ajax请求时需要设置请求头为

Content-Type: application/x-www-form-urlencoded

请求数据的内容格式是这样name=bbba&age=22
如果表单提交的话只需要设置请求编码application/x-www-form-urlencoded即可。
2、在web.xml中添加HttpPutFormContentFilter

参考 7444590

3、在web.xml中添加HiddenHttpMethodFilter

参考 7444321

I recommend using the 3rd solution, no reason, personal preference.

Configuring RESTful style controllers in Springmvc

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.