5

I'm having trouble working out why the file extension (.jpg) in the following request is being stripped before calling my controller method:

GET http://blah.com/assets/picture.jpg

I've set the content negotiation to not favour path extensions:

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = { "com.blah" })
public class PlatformWebAppConfig extends WebMvcConfigurerAdapter {    

    public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {    
        configurer.favorPathExtension(false);
        configurer.favorParameter(false);
        configurer.useJaf(false);
        configurer.defaultContentType(MediaType.APPLICATION_JSON);
    }
}

my controller is this:

@Controller
public class FileUploadRestApi {
    @RequestMapping(
        value = "/assets/{filename}",
        method = RequestMethod.GET)
    public void downloadFile(HttpServletResponse response,
        @PathVariable("filename") String filename,
        Principal principal) {
            // ERROR: 'filename' has extension stripped !!!!
    }
}

I've also tried adding the following to the PlatformWebAppConfig class above with no luck:

@Override
public void configurePathMatch(PathMatchConfigurer matcher) {
    matcher.setUseSuffixPatternMatch(true);
    matcher.setUseRegisteredSuffixPatternMatch(true);
}

I've tried to debug the issue and I've got as far as seeing ContentNegotiationManagerFactoryBean.afterPropertiesSet is definitely seeing the favorPathExtension setting as false and not setting up a content negotiation strategy (as expected).

Update:

If I change the request mapping to

"/assets/{filename:.+}"

the filename will include the extensions but this does not explain why setting all those configurers did not achieve anything????

Shane Rowatt
  • 1,951
  • 3
  • 27
  • 44
  • You should have `configurer.favorPathExtension(true);` , yes with *true*, with false you practically ignore any path extension how .xml, .json and this case .jpg – Manuel Jordan Nov 05 '15 at 16:31

0 Answers0