Spring Rest Request 406 Not Acceptable for ".123"

Viewed 1047

I have a Spring RestController with a RequestMapping and a PathVariable:

@RequestMapping(value = "/path/{someId:.+}")
public void method(@PathVariable("someId") String someId) {
   ...
}

When calling this controller, I get a Http 406 Not Acceptable error with requests like:

- /path/id8327.123
- /path/id8327.txt

But not with:

- /path/id8327.234
- /path/id8327.bbb

Isn't that strange? It was only recently I found out that .txt also failed, so I guess it has something to do with extension mappings.

How can I work around this hidden feature?

Kind regards

2 Answers

You can add a '/' at end of the URL, like:/path/id8327.123/.

This method can help Spring to recognize.

I ran into this issue while calling an endpoint that had an IP @PathVariable at the end.

Apparently, there is an older MediaType: application/vnd.lotus-1-2-3.

Spring tries to determine what the MediaType to return should be and it does this first by finding a possible MediaType extension in the URL.

In this case it finds the extension as the String after the last '.' For the '123' case Spring thinks that the MediaType should be the value for the MediaType map key '123' which is 'application/vnd.lotus-1-2-3'.

The easy fix is to change @PathVariable into @RequestParam and pass the value as an URL query parameter.

Related