@PathVariable报错Could not find acceptable representation解决方案

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u011870280/article/details/85008671

今天用@PathVariable出现了下面问题:

Could not find acceptable representation。

原因是url参数里参数是文件名,带了.符号。如下图:

@ApiOperation(value = "下载", nickname = "download")
    @RequestMapping(name = "下载", value = "download/{fileName}", method = {RequestMethod.POST})
    public ResponseEntity<BaseResult> download(@PathVariable("fileName") String fileName) {
        return ResponseEntity.ok(new BaseResult());
    }

spring mvc将文件扩展名截断,并将扩展名视为媒体类型并尝试创建所需的格式。 而不会执行对应的controler方法。

用以下配置类禁用后缀匹配和路径扩展可以解决这些问题。

@Component
@Configuration
public class PathMatchingConfigurationAdapter extends WebMvcConfigurerAdapter {
    //避免文件后缀被截断,如a.txt只接收到a
    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        configurer.setUseSuffixPatternMatch(false);
    }
    //禁用路径扩展,不用于不确定请求的媒体类型
    @Override
    public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
        configurer.favorPathExtension(false);
    }
}

猜你喜欢

转载自blog.csdn.net/u011870280/article/details/85008671
今日推荐