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.

Leave a Comment

5 comments

  1. P

    Thanks Konstantin, you saved my day !

  2. T

    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

  3. F

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

  4. Y

    I have implemented the same but still it is not working
    @SpringBootApplication
    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);
    }

    @Configuration
    @EnableWebMvc
    public static class WebMvcConfig extends WebMvcConfigurerAdapter {

    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
    configurer.setUseSuffixPatternMatch(false);
    }

    @Override
    public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
    configurer.favorPathExtension(false);
    configurer.ignoreUnknownPathExtensions(false);
    }
    }
    }

  5. K

    What error message do you get?

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close