Spring WebMVC – Bad request for most pages


Today I stumbled over an easy configuration mistake you can make, which will cause “400 Bad request” for most static resources. It took me a little bit of time to figure out what went wrong.

I had a classic Spring WebMVC setup, DispatcherServlet, resource mapping for static resources (CSS mostly).

After making a few changes all CSS files started to have “400 Bad request”. Which was strange, since these were only static resources. Bad request sounded like something went wrong in the Jetty that I used. So I started debugging into this issue.

It turned out that all requests were directed to my newly added @Controller class that already was active in the Spring context. It’s handler method was causing the “400” error. By why for all CSS files?

It was a missing “value” property in the @RequestMapping annotation. I had:

@Controller
  public class ArtifactController {
  @RequestMapping ( name = "/artifact/{artifactId}/get", method = RequestMethod.GET )
  public void get ( HttpServletResponse response,
    @PathVariable ( "artifactId" ) String artifactId ) {
    final StorageService service = Activator.getTracker ().getStorageService ();
  }
}

Where it should have been:

@Controller
public class ArtifactController {
  @RequestMapping ( value = "/artifact/{artifactId}/get", method = RequestMethod.GET )
  public void get ( HttpServletResponse response,
    @PathVariable ( "artifactId" ) String artifactId ) {
    final StorageService service = Activator.getTracker ().getStorageService ();
  }
} 

Note: the @RequestMapping attribute should have been “value” instead of “name”.