【问题标题】:How can I log the Content-Type that Spring rejects with a 415 Unknown Media Type?如何使用 415 Unknown Media Type 记录 Spring 拒绝的 Content-Type?
【发布时间】:2020-01-10 08:47:20
【问题描述】:

我有一个带有 REST 端点的基本 Spring Boot 应用程序,该端点配置为接收带有 Content-Type = application/json 的 POST。

我的外部合作伙伴正在向我发送 HTTP 请求,但显然使用了不同的 Content-Type,因为我的应用程序拒绝了这些带有 HTTP 415 Unsupported Media Type 的请求。

不幸的是,Spring 的日志没有透露 Content-Type 的确切含义,并且外部合作伙伴目前无法回答问题。

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

【问题讨论】:

  • 你不能写一个自定义拦截器吗?类似完成here
  • 没错,可以做到。把它变成一个答案,我会投票吗?不过,我希望有一个基于配置的解决方案,这将使我能够在没有代码的情况下查看更多日志详细信息,甚至可能无需重新部署。
  • 这就是为什么我将其仅列为评论,因为我也在研究您提到的内容:)。那将是可取的。你前面没有http服务器吗?或者你不能使用像wireshark 这样的外部应用程序吗?或者actuators 怎么样,应该只是配置更改?
  • 无论如何都要把它变成一个答案,我想说。并不意味着它必须是/仍然是最佳答案,对吧?研究了其他选项,但这并不容易。 [公司的东西... :-(] 有一个 HTTP 服务器,但它不在我的控制之下,我无法快速访问它,这使得检查他们的日志或添加额外的工具(如 wireshark)变得很困难。执行器已切换出于安全考虑而关闭...

标签: java spring rest spring-boot http-trace


【解决方案1】:

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

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

使用 httpTrace 执行器

httptrace 提供有关 HTTP 请求/响应交换的信息。可以通过访问 /actuator/httptrace 来调用它。可以使用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
  } ]
}

【讨论】:

    猜你喜欢
    • 2020-05-05
    • 2020-02-18
    • 1970-01-01
    • 1970-01-01
    • 2016-06-10
    • 1970-01-01
    • 2011-05-13
    • 1970-01-01
    • 2022-10-04
    相关资源
    最近更新 更多