Spring Boot practice -- the problem that the PUT request cannot receive parameters

Introduction:

The project has transformed the framework and made all the APIs of the controllers REST. If you don't do it, you don't know. SpringMVC's REST has various pits for you to jump in. It took me a lot of time to bypass them. This time, I will mention SpringMVC's PUT submits the case where the parameter is null.

However, I found a very interesting phenomenon: when the PUT parameter is received as empty, the front-end is correctly passed the value, and the back-end receiving object cannot be mapped, that is, it is empty. By printing: the content of the request.getInputStream stream, it is found that it is If there are parameters, it is not mapped in. In fact, spring is not enabled by default.

1: JSON submission method: Content-Type: application/json

   Backend: Object Reception: Except: get request, no need to add @ReqeustBody annotation, all others are required.

            Parameter reception: use: @RequestParam or not.

 Use this kind of request: When other back-end colleagues develop: when the client (SOAP) simulates the request, the @ReqeustBody parameter cannot be received, so it is removed, and the update code is added during front-end development. Because the company network cannot download the plug-in, it was replaced with: form form submission.

2: Form form submission method: Content-Type: application/x-www-form-urlencoded  

    form form data format: is: param1=111¶m2=222 This kind of opening.

Backend: Object Receiving: Except: All Requests: Not Annotated with @ReqeustBody

         Parameter reception: can use: @RequestParam or not.

 

SpringBoot solution:

//使用@bean注解
@Configuration
@EnableWebMvc
public class WebMvcConfig extends WebMvcConfigurerAdapter {
    // 就是这个
    @Bean
    public HttpPutFormContentFilter httpPutFormContentFilter() {
        return new HttpPutFormContentFilter();
    }

}

//或者使用:@Component,其实原理都是一样,就是开启:HttpPutFormContentFilter
@Component
public class PutFilter extends HttpPutFormContentFilter {
}

 

SpringMVC solution:

This way comes from: SpringMVC controller can not receive the solution of the parameters submitted by PUT

Post my controller code as usual, nothing special, just print out the received foreground parameter values: 

    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
	@ResponseBody
	public Map<String, Object> update(
			@RequestParam(value = "isform", required = false) String isform,
			@PathVariable("id") String id) {
		System.out.println("id value: " + id);
		System.out.println("isform value: " + isform);

		return null;

	}

A very conventional PUT controller is used to modify the original record. In the original web.xml, I only added a filter related to REST 

org.springframework.web.filter.HiddenHttpMethodFilter 

<filter>
		<filter-name>HttpMethodFilter</filter-name>
		<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>HttpMethodFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

This is because most people know its function, so I will mention it here: 

    The browser form only supports GET and POST requests, but DELETE, PUT and other methods are not supported. Spring 3.0 adds a filter that can convert these requests into standard http methods, making it support GET, POST, PUT and DELETE. Request, the filter is HiddenHttpMethodFilter, just add a hidden field "_method" to the form 

<form action="..." method="post">  
            <input type="hidden" name="_method" value="put" />  
            ......  
    </form>

Let's take a look below, the result of the operation, I will initiate a PUT request in my foreground as a case, 

Let's take a look at the background parameter printing: 

The id parameter is successfully obtained, because it is actually obtained by @PathVariable, there is no problem with this, but the parameter value isform submitted in the http body is null, and the query is made because:

If the PUT method is used, SpringMVC will not recognize the parameters in the request body by default, or some people say that Spirng MVC does not support PUT requests with parameters by default.

The solution is also very simple, just change the original filter in web.xml,

Replace with org.springframework.web.filter.HttpPutFormContentFilter 

<filter>
		<filter-name>HttpMethodFilter</filter-name>
		<filter-class>org.springframework.web.filter.HttpPutFormContentFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>HttpMethodFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

After the change, let's continue the case just now, send a PUT request, and the parameters are basically unchanged 

Look at the result of the background printing: 

ok, now the parameters of the foreground can be successfully obtained and printed out.

Guess you like

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