Springboot应用程序ResponseBodyAdvice未触发

时间:2018-08-21 16:01:29

标签: spring-mvc spring-boot spring-restcontroller

我正在尝试使用响应正文建议更新审核条目,但据我所知,它永远不会执行。我在日志中看到了Bean:

{"timestamp":"2018-08-21T15:48:08.349Z","level":"INFO","thread":"main",
"logger":"org.springframework.data.rest.webmvc.RepositoryRestHandlerAdapter",
"message":"Detected ResponseBodyAdvice bean in responseAuditAdvice","context":"default"}

我的控制器方法如下:

@PostMapping(path = "/stage", consumes = {
    "application/json"
}, produces = {
    "application/json"
})
@ResponseBody
public ResponseEntity<?> stage(@Valid @RequestBody StagingDto stagingDto,
                               @RequestHeader(HttpHeaders.USER_AGENT) String userAgent,
                               BindingResult bindingResult) {

我有一个RequestAuditAdvice,它扩展了RequestBodyAdviceAdapter,并且工作正常。另外,如果发生错误流,我也会看到异常建议也在执行。只是响应建议未能触发。有什么建议吗?

这是建议豆:

@Slf4j
@RequiredArgsConstructor(onConstructor_ = @Inject)
@ControllerAdvice
public class ResponseAuditAdvice implements ResponseBodyAdvice<Object> {
private final RequestService requestService;

    @Override
    public boolean supports(MethodParameter returnType, Class converterType) {
        return true;
    }

    @Override
    public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class selectedConverterType,
                              ServerHttpRequest request, ServerHttpResponse response) {
         log.info("Updating audit for response.");
         String ip = new String (request.getRemoteAddress().getAddress().getAddress());
         requestService.auditResponse(ip, 200);
         return body;
    }
}

0 个答案:

没有答案