【问题标题】:406 Not Acceptacle error while hitting REST API from postman从邮递员访问 REST API 时出现 406 Not Acceptacle 错误
【发布时间】:2019-11-25 12:38:52
【问题描述】:

我有一个 getMapping API,它总是响应 406 不可接受的错误。

@GetMapping(path="/check/{userId}", produces="text/plain")
public @ResponseBody Long check(@PathVariable("userId") String userId) {
    return (long) 22;
}

我从邮递员那里击中的端点,

localhost:8080/api/check/0003az

带有标题,

Content-Type : "application/json"
Accept : "text/plain"

来自邮递员,并得到406 Not Acceptable的回复

这里有什么我遗漏的吗?

【问题讨论】:

  • 你的意思是localhost:8080/check/0003az in @GetMapping 你没有api 除非你在控制器中提到它
  • 406 不可接受是您的客户端代理要求服务器接受标头,但它无法这样做。看看你的 Accept 头,那个 MIME 类型,是不是被服务器授权了?
  • @YCF_L 我有/api 很常见
  • @EvOlaNdLuPiZ 我正在通过 Accept heade

标签: java rest api spring-boot spring-mvc


【解决方案1】:

406 Not Acceptable 这意味着您的返回类型有问题。

请通过this链接更改您的退货(长)22;字符串或删除 Accept: "text/plain"

【讨论】:

    【解决方案2】:

    注意difference between Long and long,因为您使用Long 作为返回类型并将其转换为long

    【讨论】:

      【解决方案3】:

      由于我正在使用produces="text/plain" 并尝试返回Long,因此出现此错误。将返回类型更改为String 并将Long 结果转换为String 后,返回错误得到修复。

      【讨论】:

        猜你喜欢
        • 2023-02-15
        • 2020-07-13
        • 2017-07-13
        • 2016-12-04
        • 2016-02-21
        • 1970-01-01
        • 2019-08-05
        • 2019-01-28
        • 2013-11-15
        相关资源
        最近更新 更多