gpt4 book ai didi

java - 如何在端点函数体内检索请求方法?

转载 作者:行者123 更新时间:2023-12-01 19:43:09 25 4
gpt4 key购买 nike

假设我有一个 Controller ,这是我的端点。我该如何做与我体内相似的事情?

@RequestMapping(path = "/path/**", consumes = "application/json")
@ResponseStatus(HttpStatus.OK)
public <T> ResponseEntity<T> getResponse(@PathVariable UUID varId) {
HttpMethod httpMethod = foo();
if (httpMethod == httpMethod.GET) {
//do something
}
}

有办法做到这一点吗?

最佳答案

您可以将 HttpServletRequest 的实例注入(inject)到您的方法中。

@RequestMapping(path = "/path/**", consumes = "application/json")
@ResponseStatus(HttpStatus.OK)
public <T> ResponseEntity<T> getResponse(@PathVariable UUID varId,
HttpServletRequest httpServletRequest) {
HttpMethod httpMethod = HttpMethod.valueOf(httpServletRequest.getMethod());
if (httpMethod == httpMethod.GET) {
//do something
}
}

请注意@GetMapping , @PostMapping@RequestMapping(method="...") - 您可以指定给定端点允许使用哪些 HTTP 方法。

关于java - 如何在端点函数体内检索请求方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54595981/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com