如何获取请求的URI而不包含路径变量?

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

How to get request URI without Path Variables?

问题

The Method request.getRequestURI() returns URI with path variables.
The following example service takes Path Variable. How can I get URL that without Path Variables

@RequestMapping(value = "/getUser/{username}", method = RequestMethod.GET)
public ResponseEntity getUser(@PathVariable String username)

if i make request like this(

localhost:8080/user/getUser/tommy

)
request.getRequestURI() returns with path variable.

user/getUser/tommy

I want to get a result like this:
user/getUser/
or
user/getUser

How can I get the path without path variables?

英文:

The Method request.getRequestURI() returns URI with path variables.
The following example service takes Path Variable. How can I get URL that without Path Variables

  1. @RequestMapping(value = "/getUser/{username}", method = RequestMethod.GET)
  2. public ResponseEntity<UserDTO> getUser(@PathVariable String username)

if i make request like this(

localhost:8080/user/getUser/tommy

)
request.getRequestURI() returns with path variable.

  1. user/getUser/tommy

I want to get a result like this:
user/getUser/
or
user/getUser

How can I get the path without path variables?

答案1

得分: 0

I don't see any Spring MVC way. You can parse from one of the available HttpServletRequest methods.

For instance:

  1. @Controller
  2. public class SampleController {
  3. @GetMapping("/sample/path/{last}")
  4. public ResponseEntity<Object> sampleMethod(@PathVariable String last, HttpServletRequest request) {
  5. System.out.println(request.getContextPath());
  6. System.println(request.getRequestURI());
  7. String path = request.getServletPath();
  8. System.println(path);
  9. System.println(path.substring(0, path.lastIndexOf(last)));
  10. return ResponseEntity.ok(last);
  11. }
  12. }
  1. /context
  2. /context/sample/path/boom
  3. /sample/path/boom
  4. /sample/path/
英文:

I don't see any Spring MVC way. You can parse from one of the available HttpServletRequest methods.

For instance

  1. @Controller
  2. public class SampleController {
  3. @GetMapping(&quot;/sample/path/{last}&quot;)
  4. public ResponseEntity&lt;Object&gt; sampleMethod(@PathVariable String last, HttpServletRequest request) {
  5. System.out.println(request.getContextPath());
  6. System.out.println(request.getRequestURI());
  7. String path = request.getServletPath();
  8. System.out.println(path);
  9. System.out.println(path.substring(0, path.lastIndexOf(last)));
  10. return ResponseEntity.ok(last);
  11. }
  12. }
  1. /context
  2. /context/sample/path/boom
  3. /sample/path/boom
  4. /sample/path/

答案2

得分: 0

我通过在我的控制器类中使用 @RequestParam 而不是 @PathVariable 来解决了我的问题

示例

  1. @RequestMapping(value = "/getUser", method = RequestMethod.GET)
  2. public ResponseEntity<UserDTO> getUser(@RequestParam String username)
英文:

I solved my problem by using @RequestParam instead of @PathVariable for get services in my Controller classes

Example

  1. @RequestMapping(value = &quot;/getUser&quot;, method = RequestMethod.GET)
  2. public ResponseEntity&lt;UserDTO&gt; getUser(@RequestParam String username)

答案3

得分: -1

不可能。您必须自己编写逻辑来提取路径变量。

英文:

Not possible. You have to write logic by your self to extract the path variable.

huangapple
  • 本文由 发表于 2023年2月7日 05:03:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/75366553.html
匿名

发表评论

匿名网友

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

确定