【问题标题】:Returning custom status and code on java rest api在 java rest api 上返回自定义状态和代码
【发布时间】:2017-09-09 17:55:54
【问题描述】:

我正在寻找一种方法来获得超出通常 http 错误代码范围的自定义状态和错误代码。像这样的:

return javax.ws.rs.core.Response.status(8001).entity("Error replacing document").build();

我得到:java.lang.IllegalArgumentException:非法状态值:8001

关于如何实现这一点的任何指示?

【问题讨论】:

    标签: java rest http-status-codes rs


    【解决方案1】:

    Response.status() 中允许的状态代码是 100..599

    public ResponseBuilder status(int s) {
        if (s < 100 || s > 599) {
            throw new IllegalArgumentException("Illegal status value : " + s);
        }
        ...
    

    HTTP 中定义的状态码列表:https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html。每个状态码都有其含义和用途。

    我猜这是Can we create custom HTTP Status codes?的副本

    【讨论】:

    • 在 tomcat 中,任何错误代码都有效。在 karaf 容器中运行的 apache cxf 面临这个问题!
    • @SathishKumar 重点是,100..599 之外的代码不在 HTTP 规范中,许多人认为它们是非法的,并且违反了良好做法。
    猜你喜欢
    • 2014-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-13
    • 1970-01-01
    相关资源
    最近更新 更多