英文:
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
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("/sample/path/{last}")
public ResponseEntity<Object> 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 = "/getUser", method = RequestMethod.GET)
public ResponseEntity<UserDTO> getUser(@RequestParam String username)
答案3
得分: -1
不可能。您必须自己编写逻辑来提取路径变量。
英文:
Not possible. You have to write logic by your self to extract the path variable.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论