Returning custom status and code on java rest api

1.8k views Asked by At

I am looking for a way to have a custom status and error code that is outside the usual http range of error code. 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

Any pointers on how to accomplish this?

1

There are 1 answers

2
radoh On

Allowed status codes in Response.status() are 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. Every status code has its meaning and use.

I guess this is a duplicate for Can we create custom HTTP Status codes?