Sam*_*Dağ 1 aop spring http spring-aop spring-boot-actuator
我想在我的方面之前和之后获取请求/响应正文和标头(如果可用)或如何获取它们。
我的意思是我认为前面的注释应该适用于请求,
注释后应适用于响应。可 ?
到目前为止我已经尝试过:
我尝试了日志库,这对我来说非常复杂,我不知道如何使用它。所以我放弃了。
执行器可以做一些技巧,但我正在做额外的工作,比如端点调用多少次等,因此我不能使用执行器。
另外,我尝试获取至少如下所示的请求标头,但我认为该标头始终相同。我无法像 httpservetrequest 那样获得 httpservletresponse 。
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
.getRequest();
Run Code Online (Sandbox Code Playgroud)
那么
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 {
}
}
Run Code Online (Sandbox Code Playgroud)
我的控制器类:
@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();
}
}
Run Code Online (Sandbox Code Playgroud)
小智 5
我遇到了同样的问题,如果您有@Aspect注释@Component(或任何@Autowired候选人),您可以简单地得到HttpServletRequest这样的结果:
@Aspect
@Component
public class SomeAspect {
@Autowired
HttpServletRequest request;
@Before("...")
public void beforeAdvice(JoinPoint jp){
/* You will have the current request on the request property */
System.out.println(request.getRequestURL());
}
}
Run Code Online (Sandbox Code Playgroud)
我知道这是一个老问题,但我希望它会有所帮助。
| 归档时间: |
|
| 查看次数: |
11373 次 |
| 最近记录: |