Springboot/Spring使用@PathVariable注解参数带点号导致被截取的解决办法

一.问题描述:

形如以下请求,参数serviceAddress格式为:127.0.0.1:80,传入后参数的值会被截断。

@RequestMapping(value = "/upgrade-config/{serviceAddress}", method = RequestMethod.GET)
@ApiResponses(value = {@ApiResponse(code = 401, message = "请求未通过认证.", response =ApiException.class)})
public UpgradeConfigEntity getUpgradeConfig(@PathVariable String serviceAddress) {
    log.info("start to getUpgradeConfig, serviceAddress: {}", serviceAddress);
    return baseDataService.getUpgradeConfig(serviceAddress);
}

二.解决方案:

方案一

     在@RequestMapping的value中使用SpEL来表示,value中的{serviceAddress}换成{serviceAddress:.+}。

方案二

继承WebMvcConfigurationSupport类或者实现WebMvcConfigurer接口,重写configurePathMatch(PathMatchConfigurer configurer)()方法:

  /**
 * Spring Boot 定制URL匹配规则
 * configurePathMatch(PathMatchConfigurer configurer)函数让开发人员可以根据需求定制URL路径的匹配规则
 *
 * @param configurer PathMatchConfigurer
 */
@Override
public void configurePathMatch(PathMatchConfigurer configurer) {
    configurer
            // configurer.setUseSuffixPatternMatch(false)表示设计人员希望系统对外暴露的URL不会识别和匹配.*后缀:设置是否是后缀模式匹配,如“/user”是否匹配/user.*,默认真即匹配;
            .setUseSuffixPatternMatch(false)
            // configurer.setUseTrailingSlashMatch(true)表示系统不区分URL的最后一个字符是否是斜杠/:设置是否自动后缀路径模式匹配,如“/user”是否匹配“/user/”,默认真即匹配
            .setUseTrailingSlashMatch(true);
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。