0

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

我的意思是我认为之前的注释应该可以满足要求,

带有注释后应该可以响应。可 ?

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

我尝试了日志库,它对我来说非常复杂,我无法弄清楚如何使用它。所以我放弃了。

执行器可以发挥作用,但我正在做额外的工作,例如端点调用的次数等。因此我不能使用执行器。

我也尝试至少获得如下所示的请求标头,但我认为这些标头始终相同。我无法像 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();
}

}
4

1 回答 1

0

我认为您需要实现接口HandlerInterceptor,它将帮助您能够检查请求和响应。例如:

public class ApiMonitor implements HandlerInterceptor {

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
    // when the client access to your endpoint
}

@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) {
    // when you finished your process 
}

@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
    // after you already returned an answer to the client
}
}

如果您想在将返回的对象发送给客户端之前对其进行操作,那么您需要 AOP,是的。这是我如何在将某个对象解析为 json 之前修改某些端点上的对象的示例。

@Component
@Aspect
public class MyCustomAOPInterceptor {

/**
* These poincuts check the execution of a method in any (*) 
* class of my.package.controller and that start with
* get/list/find plus any other word (*) . For example
* my.package.controller.UserController.getUserById()
*/
@Pointcut("execution(* my.package.controller.*.get*(..))")
public void petitionsStartWithGet() { }

@Pointcut("execution(* my.package.controller.*.list*(..))")
public void petitionsStartWithList() { }

@Pointcut("execution(* my.package.controller.*.find*(..))")
public void petitionsStartWithFind() { }

@AfterReturning(pointcut = "petitionsStartWithGet() || petitionsStartWithList() || petitionsStartWithFind()", returning = "result")
public void translateEntities(JoinPoint joinPoint, Object result) {
    // do your stuff; result is the object that you need
}

}
于 2020-07-16T14:25:57.703 回答