说我有一个控制器,这是我的端点。我将如何做与体内类似的事情?

@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的实例注入您的方法。

@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方法。

09-13 12:28