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

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

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

@RequestMapping(value = "/getUser/{username}", method = RequestMethod.GET)
public ResponseEntity<UserDTO> 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?

答案1

得分: 0

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

For instance:

@Controller
public class SampleController {

	@GetMapping("/sample/path/{last}")
	public ResponseEntity<Object> sampleMethod(@PathVariable String last, HttpServletRequest request) {

		System.out.println(request.getContextPath());
		System.println(request.getRequestURI());

		String path = request.getServletPath();
		System.println(path);
		System.println(path.substring(0, path.lastIndexOf(last)));

		return ResponseEntity.ok(last);
	}
}
/context
/context/sample/path/boom
/sample/path/boom
/sample/path/
英文:

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

For instance

@Controller
public class SampleController {

	@GetMapping(&quot;/sample/path/{last}&quot;)
	public ResponseEntity&lt;Object&gt; sampleMethod(@PathVariable String last, HttpServletRequest request) {

		System.out.println(request.getContextPath());
		System.out.println(request.getRequestURI());

		String path = request.getServletPath();
		System.out.println(path);
		System.out.println(path.substring(0, path.lastIndexOf(last)));

		return ResponseEntity.ok(last);
	}
}
/context
/context/sample/path/boom
/sample/path/boom
/sample/path/

答案2

得分: 0

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

示例

@RequestMapping(value = "/getUser", method = RequestMethod.GET)
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

@RequestMapping(value = &quot;/getUser&quot;, method = RequestMethod.GET)
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:

确定