@PathVariable 带"."号传参的小坑

一、问题 
项目使用SpringBoot时,使用 注解接收参数,前端页面传递参数值为100.01,但是后台接收参数为100,而且通过request去获取url时就是100.01 ,所以怀疑是该注解注入值导致的,通过网上找了一些资料,发现了解决办法

后台接收的参数

Controller:

@RequestMapping("/toUpper/{amount}")
@ResponseBody
public String toUpper(@PathVariable("amount") String amount) {
  •  

二、解决方案

1.在version段后增加一个静态的字符段,这个段没有任何意义,可以为任意字符。

URL:

http://localhost:8070/amount/toUpper/100/suibianjia?t=1517391101418
  •  

代码

@RequestMapping("/toUpper/{amount}/suibianjia")
@ResponseBody
public String toUpper(@PathVariable("amount") String amount) {

问题虽然解决了,但毕竟URL后多了个小尾巴,心情不愉悦。 
再次谷哥。 
参考: 
http://stackoverflow.com/questions/3526523/spring-mvc-pathvariable-getting-truncated

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

@RequestMapping("/toUpper/{amount:.+}")
@ResponseBody
public String toUpper(@PathVariable("amount") String amount) {

     

猜你喜欢

转载自my.oschina.net/u/2353881/blog/1616287