0

I have defined the below controller

@Controller

public class HelloController {

@RequestMapping(value = "/config/{name:.*}", produces = MediaType.TEXT_PLAIN_VALUE, method = RequestMethod.GET)
@ResponseBody
ResponseEntity<String> getValue(@PathVariable String name) {
    String value = "Hello World";
    return new ResponseEntity<String>(HttpStatus.OK);
}

}

when i ping the url from the browser ex: http://localhost:8080/example/config/test.abc

The request works fine.

But when i ping with url http://localhost:8080/example/config/test.uri

it just blows the page with the error: The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers.

I tried MessageConverters and configureContentNegotiation nothing seems to be working. I am wondering if spring treats test.uri as an invalid patterns or reserved keywords.

Environments i tried. Spring 4/Tomcat 7 & 8 Spring 5/ Tomcat 9

3 Answers3

2

try a different regex.

instead of

.*

which means: a various number of any characters except new line

try

[a-z]*\.[a-z]*

which means a various number of a-z + a dot + a various number of a-z

if this is what you want.

if you dont need any pattern then just use

{name}

Checkout

https://www.logicbig.com/tutorials/spring-framework/spring-web-mvc/uri-pattern.html

and

https://regexr.com/

But I would consider if you could adjust your API like:

@RequestMapping(value = "/config/{name}/{type}", ...

I think it is not a good idea to expect dots in your URI. Dots means that you are requesting a file.

Checkout:

Spring MVC @PathVariable with dot (.) is getting truncated

DCO
  • 1,222
  • 12
  • 24
  • Could you please add explanation why test.test, test.url is working but test.uri not working ? in the answer – IMParasharG Mar 21 '19 at 15:41
  • I tried {name} and [a-z]*\.[a-z]* no much luck. i cannot go the pattern of @RequestMapping(value = "/config/{name}/{type}" as that was a very big change for us – praveen kotla Mar 21 '19 at 17:09
  • Then maybe try /somepath/{variable:.+} as described in my last link – DCO Mar 22 '19 at 07:58
0

Spring considers that anything behind the last dot is a file extension

To overcome this

modify our @PathVariable definition by adding a regex mapping

@RequestMapping(value = "/config/{name:.+}"

And a safe guess would be

.abc is no extension type and .uri is one extension so maybe that's why your first URL works,

MyTwoCents
  • 7,284
  • 3
  • 24
  • 52
0

i finally could able to fix the problem using ContentNegotiationConfigurer and setting the content type for uri.

    @Override
public void configureContentNegotiation(ContentNegotiationConfigurer contentNegotiationConfigurer) {
    contentNegotiationConfigurer.mediaType("uri", MediaType.TEXT_PLAIN);
}