PUT 请求:缺少必需的请求正文

PUT request: Required request body is missing

鉴于此 Spring 启动应用程序:

@SpringBootApplication
@RestController
public class ShowCase {

    public static void main(String[] args) {
        SpringApplication.run(ShowCase.class, args);
    }

    @RequestMapping(value = "submit", method = RequestMethod.PUT, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public void getFormKeys(@RequestBody MultiValueMap<String, String> formData) {
        System.out.println(formData.keySet().stream().collect(joining(",")));
    }

}

这个 curl 请求:

curl -X PUT -H "Content-Type: application/x-www-form-urlencoded" --data "arg1=val&arg2=val" http://localhost:8080/submit

使用 Spring Boot 1.2.5 可以正确调用该方法并打印表单键。

使用 Spring Boot 1.3.8 不会调用该方法,而是会记录一条警告:

WARN 17844 --- [nio-8080-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing: public void web.ShowCase.getFormKeys(org.springframework.util.MultiValueMap<java.lang.String, java.lang.String>)

在 Spring Boot 1.3.8 中需要什么才能使此 PUT 请求再次工作?

添加@EnableWebMvc注解:

@SpringBootApplication
@RestController
@EnableWebMvc
public class ShowCase {

    public static void main(String[] args) {
        SpringApplication.run(ShowCase.class, args);
    }

    @RequestMapping(value = "submit", method = RequestMethod.PUT, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public void getFormKeys(@RequestBody MultiValueMap<String, Object> formData) {
        System.out.println(formData.keySet().stream().collect(Collectors.joining(",")));
    }

}