小编Sam*_*Dağ的帖子

Spring AOP 中获取请求/响应正文和标头

我想在我的方面之前和之后获取请求/响应正文和标头(如果可用)或如何获取它们。

我的意思是我认为前面的注释应该适用于请求,

注释后应适用于响应。可 ?

到目前为止我已经尝试过:

我尝试了日志库,这对我来说非常复杂,我不知道如何使用它。所以我放弃了。

执行器可以做一些技巧,但我正在做额外的工作,比如端点调用多少次等,因此我不能使用执行器。

另外,我尝试获取至少如下所示的请求标头,但我认为该标头始终相同。我无法像 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 = …
Run Code Online (Sandbox Code Playgroud)

aop spring http spring-aop spring-boot-actuator

1
推荐指数
1
解决办法
1万
查看次数

标签 统计

aop ×1

http ×1

spring ×1

spring-aop ×1

spring-boot-actuator ×1