如何在SpringBoot中替换PathVariable中的字符

huangapple go评论58阅读模式
英文:

How to replace characters at PathVariable in SpringBoot

问题

我尝试了几种方法,但未能解决。我正在寻找一种解决方案,可以在Spring Boot的Path Variable中替换特殊字符。

示例:

xyz.com/3233+23+232+323

我正在寻找一种可能的解决方案,其中Spring的@PathVariable将把字符串“323323232323”返回给我,不带 +号

我知道我可以进行简单的字符串替换,但有数百个API,这将是困难的。

我正在寻找一些需要最小更改的东西。

英文:

I tried a couple of solutions but was unable to solve it. I looking for a solution where I can replace special characters in Path Variable in Spring Boot.

Example

xyz.com/3233+23+232+323

I am looking for a possible solution where Spring @PathVariable returns me the String "323323232323" without + sign.

I know I can do a simple String replace, but there are 100's of API's and it will be difficult to do that.

I am looking for something with minimal changes required.

答案1

得分: 1

一种方法,只需读取 @PathVariable 的值,然后应用正则表达式。

只需一行,示例:

@GetMapping("/read/{str}")
public String check(@PathVariable String str){
    String modifiedStr = str.replaceAll("\\+", "");
    return modifiedStr;
}

示例: http://localhost:8080/ticket-service/read/3233+23+232+323

输出: 323323232323

英文:

One way, just read the @PathVariable value, then apply a regular expression.

Just One line, Example:

       @GetMapping("/read/{str}")
        public String check(@PathVariable String str){
            String modifiedStr = str.replaceAll("\\+", "");
            return modifiedStr;
        }

Sample: http://localhost:8080/ticket-service/read/3233+23+232+323

Output: 323323232323

huangapple
  • 本文由 发表于 2020年9月18日 02:58:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/63944604.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定