4

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

codesmith
  • 1,381
  • 3
  • 20
  • 42
  • 1
    Possible duplicate of [Spring MVC @PathVariable with dot (.) is getting truncated](https://stackoverflow.com/questions/16332092/spring-mvc-pathvariable-with-dot-is-getting-truncated). That thread should help you. .123 and .txt are registered mime/content types. One of the answers shows you how to turn this off. – Robert Moskal Jul 19 '17 at 11:39

2 Answers2

4

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

This method can help Spring to recognize.

anothernode
  • 5,100
  • 13
  • 43
  • 62
Null
  • 41
  • 2
1

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.

bogdan
  • 11
  • 1