从URL中获取值

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

How to get the value from the URL

问题

我想动态从URL中获取数值。你能帮助实现吗?

请求URL:

requestForm/intakeReqDetails/3210/request

期望响应:

3210

我可以使用子字符串方法来实现这个,但索引号码应该是硬编码的。你能否提供更好的解决方案?

英文:

I want to get the value from the URL dynamically. Could you please help achieve this?

Request URL:

requestForm/intakeReqDetails/3210/request

Expected Reponse:

3210

I can achieve this using the substring method, but the index number should be hardcoded. Can you please get a better solution?

答案1

得分: 1

我认为使用这种方法你可以获取你想要的:

https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/util/UriComponents.html#getPathSegments()

英文:

I think with this method you can retrieve what you want :

https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/util/UriComponents.html#getPathSegments()

答案2

得分: 0

我将完成@Guillaume的回复,他建议使用一个Spring工具类,但在Java中,我们有Class URL,其中包含一些非常好的方法,只需查看获取方法。

或者@Jorn建议如何使用@PathVariable

@GetMapping(path = "/requestForm/intakeReqDetails/{messageId}/request")
public String getInfo(@PathVariable Integer messageId) {
    return "message id: " + messageId;
}
英文:

I will complete @Guillaume response, he suggest to use a spring util class but in java we have Class URL with such a great methods, just have a look at get methods.

Or how @Jorn suggest use @PathVariable

@GetMapping(path = "/requestForm/intakeReqDetails/{messageId}/request")
public String getInfo(@PathVariable Integer messageId) {
 return "message id: " + messageId;
}

huangapple
  • 本文由 发表于 2023年7月31日 18:59:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76802964.html
匿名

发表评论

匿名网友

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

确定