首页 > 解决方案 > 从 Spring Boot 应用程序中的请求和返回值派生内容类型

问题描述

在 Spring boot 应用程序中,通过 Aspect,我想要 HttpRequest 和 HttpResponse 的内容类型。基本上,我想确定关联的内容类型是否为application/json.

到目前为止,我已经编写了以下代码片段:

public void captureRequest(JoinPoint joinPoint, Object returnValue)  {
    HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
      .currentRequestAttributes())
      .getRequest();

    String requestContentType = ????   <--CANNOT UNDERSTAND WHAT TO WRITE HERE
    if(requestContentType.equals("application/json") {
    ...
    }
    String responseContentType = ??? <-- CANNOT UNDERSTAND HOW TO DERIVE CONTENT TYPE FROM RETURN VALUE
  }

编辑

在这方面,在浏览了这里关于 SO 的几篇文章之后,我想,会有一个关键:Content-Type作为标题中的关键之一。但是当我按如下方式打印标题名称时,我没有找到任何 key Content-Type。我只找到以下键:

Cookie
Accept
Connection
User-Agent
Sec-Fetch-Site
Sec-Fetch-Dest
Host
Accept-Encoding
Sec-Fetch-Mode
Accept-Language

如前所述,在上面的代码片段中,我想派生请求和返回值的内容类型。有人可以帮忙吗?

标签: javaspring-bootaspectjspring-aop

解决方案


推荐阅读