08. December 2016
2 min

Spring Boot: Passing URIs as REST Path Parameters

Currently, I am setting up a RESTful service with Spring Boot that indexes files and authors of a Git repository. For example, I have a method listAuthors(String filename) that returns a collection of every person who committed changes for the given file. When exposing that method via REST, passing the filename as a path parameter, I encountered the problem that some files (e.g. /authors/pom.xml) cause errors due to their extensions.

The first problem was caused by Spring truncating everything after the last dot (.) in the URL. The reason for this seems to be that Spring considers the part after the last dot as a file extension and does not pass it as part of the path parameter.

My first solution for this was to use a regular expression when specifying the path parameter:

This works because this regex accepts everything, including the last dot. But I have also found a, in my opinion, more elegant solution:

By default the value is true and a method mapped to “/text” also matches to “/text.*”. By setting the value to false Spring distinguishes between “/text” and “/text.*” and as a result file extensions are not truncated anymore. More information about the class PathMatchConfigurer can be found here.

Another error occurred by using special file extensions like .xml or .pdf. I used the method listAuthors again to access the file pom.xml. I received the following error message:

The reason for this is that Spring considers the suffix as a media type and attempts to create the required format. In most cases this leads to an error because Spring does not know how to represent the collection. You can have a look at the Spring Blog to get more detailed information about the content negotiation.

To fix this problem I found a way to disable that spring favors path extensions by extending my Application class:

By setting .favorPathExtension(false), the path extensions are disabled and are not used to determine the requested media type. More information about the class ContentNegotiationConfigurer can be found here.

Comment article


  1. Konstantin

    What error message do you get?

  2. Yogendra

    I have implemented the same but still it is not working
    public class SpringBootWebApplication extends SpringBootServletInitializer {
    static Logger log = LoggerFactory.getLogger(“SpringBootWebApplication”);

    public static void main(String[] args) {
    log.debug(“Entitlements Spring Boot App starting”);
    SpringApplication.run(SpringBootWebApplication.class, args);

    public static class WebMvcConfig extends WebMvcConfigurerAdapter {

    public void configurePathMatch(PathMatchConfigurer configurer) {

    public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {

  3. Ferrair

    Wonderful! Thanks Konstantin. I have been crazy about my 406 error.

  4. Tanya

    Thank you so much! I was at my wits’ end trying to figure out why i get the 406 error while explicitly building and declaring a JSON response

  5. Prateek

    Thanks Konstantin, you saved my day !