Talking about GET, POST, PUT sending request

Generally, after we enter the URL in the address bar of the browser and press Enter, a GET request is sent.

When submitting a form (the most common is user login), we can specify whether it is a GET or POST request. It should be noted here that some browsing The server does not support PUT requests, so the method corresponding to the PUT request of the background restful can't use

SpringMVC to provide us with a solution,

quote
<form id="user" action="/springmvc/user" method="post"> 
    <input type="hidden" name="_method" value="put"/> 
</form>


We still use the POST method, but we add a hidden field named _method with the value put, and we need to add a Filter in web.xml, as follows:

quote
<filter>
    <filter-name>HiddenHttpMethodFilter</filter-name>
    <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>HiddenHttpMethodFilter</filter-name>
    <servlet-name>mcpMvc</servlet-name>
</filter-mapping>


In this way, although the POST request is still sent, the Filter will convert it into a PUT request so that the corresponding method of PUT can be

called . The disadvantage of this is that we need to add an additional hidden domain to the front-end page. Let's

discuss JQuery with springMVC Some things about sending AJAX requests

Generally , the code we send requests is as follows:

quote
$.ajax({ 
        type : 'get', 
        url : 'http://localhost/TestSpring3.2R/coc/body?name=tom'
    });


or as follows:

quote
var mydata = {'name':'tom'};
$.ajax({ 
        type : 'get', 
        url : 'http://localhost/TestSpring3.2R/coc/body',
        data : mydata      
    });


So what is the difference between these two?
After testing, it is found that there is no difference between the 2 methods for GET and POST,

but for PUT, the first parameter can be passed in smoothly, but the second parameter cannot be accepted, then we How can I get the parameters worn by the second way?

Here we can use @RequestBody

@RequestBody is meaningless for GET requests

For POST requests, whether you use the first way or the second way, XOR 2 If these methods are used at the same time, he will put all the parameters into the parameter corresponding to @RequestBody, and the output result is similar to:

quote
id=ff&status=gg&name=ee


If you want to use parameters, you need to handle them

yourself . The value of the parameter corresponding to the PUT request @RequestBody is the value passed in through the second method

---------------- -------------------- I am the dividing line -------------------------- ------------

For SpringMVC, it provides the MultiValueMap class, we can accept parameters like this

quote
@RequestMapping(value = "/", produces = "application/json;charset=UTF-8", method = RequestMethod.PUT)
@ResponseBody
public Map<String,Object> updateUser(@RequestBody MultiValueMap<String, String> valueMap){


He returns the parameters you passed in and puts them in the valueMap. This interface inherits from Map<K, List<V>>, the key is the passed parameter name, and the value is the parameter value. Since there may be parameters with the same name, the value The type is List, so we can easily get the parameter value.

Reference : https://my.oschina.net/dxqr/blog/113766

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327031254&siteId=291194637