springboot 请求路径有后缀_Springboot/Spring使用@PathVariable注解参数带点号导致被截取的解决办法...

当Springboot应用中使用@PathVariable接收如'127.0.0.1:80'这样的参数时,点号可能导致值被截断。可以通过两种方案解决:一是在@RequestMapping的value使用SpEL表达式{serviceAddress:.+};二是重写configurePathMatch方法,设置setUseSuffixPatternMatch(false)和setUseTrailingSlashMatch(true)来定制URL匹配规则。
摘要由CSDN通过智能技术生成

一.问题描述:

形如以下请求,参数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);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值