我想在我的方面之前和之后获取请求/响应正文和标头,如果它可用或如何获取这些。
我的意思是我认为之前的注释应该可以满足要求,
带有注释后应该可以响应。可 ?
到目前为止我已经尝试过:
我尝试了日志库,它对我来说非常复杂,我无法弄清楚如何使用它。所以我放弃了。
执行器可以发挥作用,但我正在做额外的工作,例如端点调用的次数等。因此我不能使用执行器。
我也尝试至少获得如下所示的请求标头,但我认为这些标头始终相同。我无法像 httpservetrequest 那样获得 httpservletresponse。
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
.getRequest();
那么
request.getHeader("date")
requestbody 呢?
如何获得请求体?响应体?响应头?
我的方面文件:
@Aspect
@Component
public class AppAspect implements ResponseInfo{
@Before("execution(@(@org.springframework.web.bind.annotation.RequestMapping *) * *(..))")
public void loggingStartPointRequests(JoinPoint joinPoint) {
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
.getRequest();
}
@After("execution(@(@org.springframework.web.bind.annotation.RequestMapping *) * *(..))")
public void loggingEndPointRequests(JoinPoint joinPoint) throws IOException {
}
}
我的控制器类:
@RestController
public class MainController {
@GetMapping("/people") //
public ResponseEntity<Poeple> getAllPeople(@RequestParam(name = "page", required = false) Integer page,
@RequestParam(name = "size", required = false) Integer size,
@RequestParam(name = "sortBy", required = false) Boolean sortByNameOrEpCount) {
doSomething();
}
}