'Getting 404 when trying to download file without extension in Spring Boot 2.1

when I try to download some file without extension I get [http-bio-8443-exec-8] WARN o.s.w.s.PageNotFound - No mapping found for HTTP request with URI [server/directory/README-325] in DispatcherServlet with name 'download-rest' . This README-325 file is a file without extension. When we try to download file with extension everything is okay, but as I said problem are those files without extension. When we add extension on this file README-325 then this controller is called

@RequestMapping(value = "/download")
    @ResponseBody
    public void fileDownload(HttpServletRequest request, HttpServletResponse response, @RequestParam(required = true, value = "server") String server, @RequestParam(required = true, value = "directory") String directory, @RequestParam(required = true, value = "fileName") String fileName) throws IOException {}

But when file is without extension, then no controller is not called. Does someone know why? NOTE: No solution from similar questions did not help us. Version of Spring is 3.2.3.Release and Spring Boot version is 2.1.



Solution 1:[1]

Spring's dispatchservlet (the class responsible for routing requests towards your controller) behaves differently when the last part of the url contains an extension (dot + letters) versus no extension. In effect, without the extension, Spring is really certain you're not trying to get a file.

Here is a class I used to configure Spring to stop threating urls with an extension as something special (because my paths could contain e-mailaddresses). This is not what you are looking for - because you do like Spring's automatic ability to fetch files for you - but it highlights how Spring tries to resolve extension in a different way.

@Configuration
public class SpringWebConfig implements WebMvcConfigurer
{
    //Allows url to end with a suffix (e.g.: /users/[email protected]). Without this configuration,
    //the ".com" suffix would be automatically removed from the path variable.
    @Override
    public void configurePathMatch(PathMatchConfigurer matcher)
    {
        matcher.setUseSuffixPatternMatch(false);
    }

    //Allows url to end with the ".com" suffix with Spring NOT throwing a "http media type" error.
    //See: https://blog.georgovassilis.com/2015/10/29/spring-mvc-rest-controller-says-406-when-emails-are-part-url-path/
    @Override
    public void configureContentNegotiation(ContentNegotiationConfigurer contentNegotiationConfigurer)
    {
        //Don't look at the url for determining the response media type
        contentNegotiationConfigurer.favorPathExtension(false);

        //If the request header says it wants a response type, try to give it.
        contentNegotiationConfigurer.ignoreAcceptHeader(false);

        //Otherwise, return json.
        contentNegotiationConfigurer.defaultContentType(MediaType.APPLICATION_JSON);
    }
}

Solution 2:[2]

Looking at the error message it seems that the filename is passed as part of the request path:

No mapping found for HTTP request with URI [download_path/README-325]

But looking at the definition of your controller it should be passed as a request parameter like:

/download?server=some-server&directory=some-directory&filename=README-325

So you either need to dapt the way you call your service or the implementation of your controller.

Edit:

@RequestParam are passed at the end of the URL, after the '?' and separated with '&'. But you are sending these values in the path, in your controller, you would need @PathVariable for that.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 user1884155
Solution 2