5

Given this Spring Boot application:

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

}

And this curl request:

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

With Spring Boot 1.2.5 the method is invoked correctly and prints the form-keys.

With Spring Boot 1.3.8 the method is not invoked, but a warning is logged instead:

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>)

What is necessary in Spring Boot 1.3.8 so that this PUT request is working again?

Roland Weisleder
  • 9,668
  • 7
  • 37
  • 59

1 Answers1

4

Add the @EnableWebMvc annotation:

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

}
  • It works. Is there a reason why this annotation is required now or what it changes? – Roland Weisleder Jan 26 '17 at 12:17
  • From the spring-boot documentation: `Normally you would add @EnableWebMvc for a Spring MVC app, but Spring Boot adds it automatically when it sees spring-webmvc on the classpath. This flags the application as a web application and activates key behaviors such as setting up a DispatcherServlet.`[https://spring.io/guides/gs/spring-boot/](source) – oruckdeschel Jan 26 '17 at 12:21
  • I think they removed it because will only use this annotation when you want to take full control of your mvc configuration, avoiding some default behaviors configured by spring boot. – Alessandro Moraes Jan 26 '17 at 12:37