我编写了一个项目,其中字符串以相反的方式返回。
@PostMapping("/reverse")
public String reverseList(@RequestBody String string) {
List<String> stringList = Arrays.asList(string.split("[+,]"));
return IntStream.range(0, stringList.size())
.mapToObj(i -> stringList.get(stringList.size() - 1 - i))
.collect(Collectors.joining("+"));
}
通过curl命令:
curl -H "Content-Type: application/json" -d "a1+a2+a3+a4" localhost:8080/hello/reverse
Output :
a4+a3+a2+a1
我该如何改变才能增加价值。例如,返回时
а1 = 10
а2 = 10
a3 = 10
当我写下以下命令时:
curl -H "Content-Type: application/json" -d "a1+a2+a3" localhost:8080/hello/reverse
它应返回总和 30。
尝试这样做。这将使您的工作更加轻松。
Send a1=10+a2=10+a3=10
代替a1+a2+a3
在curl命令中。
命令 :
curl -H "Content-Type: application/json" -d "a1=10+a2=10+a3=10" localhost:8080/hello/reverse
将代码更新为:
@PostMapping("/reverse")
public String reverseList(@RequestBody String str) {
int sum = 0;
String[] variables = str.split("\\+");
for (String variable : variables) {
sum += Integer.parseInt(variable.split("=")[1]);
}
return String.valueOf(sum);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)