英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论