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

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.