如何记录 Spring 使用 415 未知媒体类型拒绝的内容类型?

How can I log the Content-Type that Spring rejects with a 415 Unknown Media Type?

我有一个基本的 Spring 启动应用程序,它有一个 REST 端点,配置为接收 Content-Type = application/json.

的 POST

我的外部合作伙伴正在向我发送 HTTP 请求,但显然使用的是不同的 Content-Type,因为我的应用拒绝了那些带有 HTTP 415 不支持媒体类型的请求。

不幸的是,Spring 的日志没有揭示 Content-Type 的确切内容,外部合作伙伴目前无法回答问题。

有没有办法提高 Spring 的日志级别,以便日志还包括收到的 Content-Type 被拒绝的消息?

我在上面的评论中看到了以下选项:

  1. 使用外部数据包分析器,例如 wireshark
  2. 使用httptrace执行器可以看例子here
  3. 编写一个拦截器,如你所见here

使用 httpTrace 执行器

httptrace provides information about the HTTP request/response exchange. It can be called by doing a get to /actuator/httptrace. One can use curl:

$ curl 'http://localhost:8080/actuator/httptrace' -i -X GET

或直接从您的浏览器,在您的本地计算机上 http://localhost:8080/actuator/httptrace

信息提供为JSON:

HTTP/1.1 200 OK
Content-Type: application/vnd.spring-boot.actuator.v3+json
Content-Length: 503

{
  "traces" : [ {
    "timestamp" : "2019-12-06T06:13:02.341Z",
    "principal" : {
      "name" : "alice"
    },
    "session" : {
      "id" : "41a5c57b-112a-4b15-8ea9-05c5942e7e88"
    },
    "request" : {
      "method" : "GET",
      "uri" : "https://api.example.com",
      "headers" : {
        "Accept" : [ "application/json" ]
      }
    },
    "response" : {
      "status" : 200,
      "headers" : {
        "Content-Type" : [ "application/json" ]
      }
    },
    "timeTaken" : 1
  } ]
}