Configuring RESTful style controllers in springmvc

In general http requests, only get and post are needed to meet the project requirements, and why use restful may be to make the request url look more intuitive and good-looking. .

Commonly used request methods for restful: get, post, put, patch, delete

By default, springmvc supports get and post the best. Like put, putch, and delete are also supported, but by default only supports the controller to receive the parameters passed by the url. If we want to pass the parameters like post, we need to We do some settings on the back-end and front-end, such as manually setting the content type in the request header to json when making a front-end request, and the back-end also needs to cooperate with the front-end to set the parameters of the controller to receive a request body instead of ordinary form or url parameters. Using the requestBody annotation to receive, springmvc will help the conversion of the json format request body to the java bean through the message processor, which is extremely troublesome, so the following three solutions are provided:

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

参考 https://blog.csdn.net/geloin/article/details/7444590

3、在web.xml中添加HiddenHttpMethodFilter

参考 https://blog.csdn.net/geloin/article/details/7444321

I prefer to use the 3rd solution, no reason, personal preference. .

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325070712&siteId=291194637