简体   繁体   English

在Java Rest API上返回自定义状态和代码

[英]Returning custom status and code on java rest api

I am looking for a way to have a custom status and error code that is outside the usual http range of error code. 我正在寻找一种方法,以使自定义状态和错误代码不在错误代码的常规http范围内。 Something like this: 像这样:

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

I get: java.lang.IllegalArgumentException: Illegal status value: 8001 我得到:java.lang.IllegalArgumentException:非法状态值:8001

Any pointers on how to accomplish this? 关于如何实现这一目标的任何指示?

Allowed status codes in Response.status() are 100..599 : Response.status()中允许的状态代码为100..599

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

The list of defined status codes in HTTP: https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html . HTTP中定义的状态代码列表: https : //www.w3.org/Protocols/rfc2616/rfc2616-sec10.html Every status code has its meaning and use. 每个状态码都有其含义和用途。

I guess this is a duplicate for Can we create custom HTTP Status codes? 我猜这是重复的, 我们可以创建自定义HTTP状态代码吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM