RestTemplate.getForEntity 将删除文件扩展名?

RestTemplate.getForEntity will remove file extension?

您好,我正在编写一个应用程序,使用户能够上传文件,然后再下载。我发现文件扩展名将从 restTemplate.getForEntity 中删除,因此 RequestMapping 不会获得完整的文件名。有人可以确认这是正确的吗?如何克服这个问题?

控制器:

@RequestMapping(value = "/uploaded/{filename}", method = RequestMethod.GET)
public ResponseEntity<Resource> getUploaded(@PathVariable String filename) {
    logger.debug("getUploaded filename=" + filename);

    Resource file = storageService.loadAsResource(filename + ".png");

    return ResponseEntity.ok().body(file);
}

单元测试:

    System.err.println("imageUpload = " + imageUpload);
    ResponseEntity<Resource> responseResource = this.restTemplate.getForEntity("/uploaded/" + imageUpload, Resource.class);
    assertNotNull(responseResource);
    System.err.println("responseResource = " + responseResource.toString());
    assertEquals(HttpStatus.FOUND, responseResource.getStatusCode());
    assertEquals(resource, responseResource.getBody());

记录显示:

imageUpload = favicon1.png
getUploaded filename=favicon1
responseResource = <200 OK,Byte array resource [resource loaded from byte array],{Content-Type=[image/png], Content-Length=[8971], Date=[Mon, 02 Oct 2017 01:58:39 GMT]}>

如您所见,imageUpload 是一个带有“.png”扩展名的文件名,并正确传递给 restTemplate.getForEntity() 但 getUploaded() 只能接收没有扩展名的文件名,因此我必须手动添加 ' .png' 这是丑陋的。所以我不知道我的实现是否有任何问题,或者其他人如何解决这个应该是通用的文件扩展名问题。

最终解决方案:

@Configuration
public class WebMvcConfiguration extends WebMvcConfigurerAdapter {
    @Override
    public void configurePathMatch(PathMatchConfigurer matcher) {
        matcher.setUseRegisteredSuffixPatternMatch(true);
    }
}