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

时间:2017-01-26 11:14:56

标签: java spring spring-boot spring-web

鉴于此Spring Boot应用程序:

@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 -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请求再次起作用?

1 个答案:

答案 0 :(得分:4)

添加@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(",")));
    }

}
相关问题