return custom error codes to client in the response for a JAX-RS

Harshawardhan

I am new to JAX-RS and still learning it, so my question can be a little naive so please bear with it :)

I have been going through various SO questions for "error codes for http response" . I have found out that as long as one dont want to be pedantic he can return 4xx status codes in the response.

I have also gone through this link RFS 2616 also the W3

I wanted to know is there any general practice to return custom error codes other than standard HTTP response status codes.

e.g if in post request for getting user information by userid and return error if userid is invalid / doesnt exists. In this case HTTP status 422 sounds idle. But if detailed error description is needed can custom error like 40xx where xx is value other than http errors be returned to client.

similarly can 40xx be defined and returned in response to client ? ( having correct syntax in the request )

4001 - userid invalid

4002 - post id invalid

etc..

or should I return 4xx status code and in the response body add by own codes for more error details?

Michal Gajdos

If you're developing internal system that won't go public then you can go with custom codes. On the other hand if you want to expose some APIs to public it's better to stick to the standard codes and provide more information in status reason phrase or in entity body.

In JAX-RS you can create your own statuses (or override reason phrases of standard statuses) like:

public static class CustomBadRequestType implements Response.StatusType {

    @Override
    public int getStatusCode() {
        return 400;
    }

    @Override
    public String getReasonPhrase() {
        return "My Reason";
    }

    @Override
    public Response.Status.Family getFamily() {
        return Response.Status.Family.CLIENT_ERROR;
    }
}

and then set the status to the Response.ResponseBuilder:

Response.status(new CustomBadRequestType()).build();

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Handling custom error response in JAX-RS 2.0 client library

From Java

Read response body in JAX-RS client from a post request

From Dev

Java jax-rs client response entity generic

From Dev

How to return List<String> as Response in JAX-RS

From Dev

How to return Response in XML in JAX-RS web service?

From Dev

JAX-RS Custom JSON representation of primitive Resource return type

From Dev

JAX-RS Custom JSON representation of primitive Resource return type

From Dev

Postman client not able to parse string response from JAX-RS response builder

From Dev

Jax-RS Multipart response

From Dev

JAX RS Client API interceptor

From Dev

JAX-RS Return Types

From Dev

JAX-RS Jersey client gets 400 response when web method parameters are annotated

From Dev

Using jax-rs to redirect get request and return response to solve CORS issue?

From Dev

How I return HTTP 404 JSON/XML response in JAX-RS (Jersey) on Tomcat?

From Dev

How can I return the location of http created resource along with a response status message in JAX-RS?

From Dev

Create Response with Location header in JAX-RS

From Dev

JAX-RS: Convert Response to Exception

From Dev

Capture Response Payload in JAX-RS filter

From Dev

JAX-RS: Convert Response to Exception

From Dev

Retrieving String from JAX-RS Response

From Dev

Client side authentication with jax-rs and cxf

From Dev

Is JAX-RS Client Thread Safe

From Dev

Read Map entity in JAX-RS client

From Dev

Client side authentication with jax-rs and cxf

From Dev

NancyFX - Return custom error response on uncaught exception

From Dev

JAX-RS Custom @Provider for a subset of the type

From Dev

JAX-RS 2.0 Client - send multipart message with RESTEasy client

From Dev

Jersey Client / JAX-RS and optional (not default) @QueryParam (client side)

From Dev

Jersey JAX RS error - No provider classes found

Related Related

  1. 1

    Handling custom error response in JAX-RS 2.0 client library

  2. 2

    Read response body in JAX-RS client from a post request

  3. 3

    Java jax-rs client response entity generic

  4. 4

    How to return List<String> as Response in JAX-RS

  5. 5

    How to return Response in XML in JAX-RS web service?

  6. 6

    JAX-RS Custom JSON representation of primitive Resource return type

  7. 7

    JAX-RS Custom JSON representation of primitive Resource return type

  8. 8

    Postman client not able to parse string response from JAX-RS response builder

  9. 9

    Jax-RS Multipart response

  10. 10

    JAX RS Client API interceptor

  11. 11

    JAX-RS Return Types

  12. 12

    JAX-RS Jersey client gets 400 response when web method parameters are annotated

  13. 13

    Using jax-rs to redirect get request and return response to solve CORS issue?

  14. 14

    How I return HTTP 404 JSON/XML response in JAX-RS (Jersey) on Tomcat?

  15. 15

    How can I return the location of http created resource along with a response status message in JAX-RS?

  16. 16

    Create Response with Location header in JAX-RS

  17. 17

    JAX-RS: Convert Response to Exception

  18. 18

    Capture Response Payload in JAX-RS filter

  19. 19

    JAX-RS: Convert Response to Exception

  20. 20

    Retrieving String from JAX-RS Response

  21. 21

    Client side authentication with jax-rs and cxf

  22. 22

    Is JAX-RS Client Thread Safe

  23. 23

    Read Map entity in JAX-RS client

  24. 24

    Client side authentication with jax-rs and cxf

  25. 25

    NancyFX - Return custom error response on uncaught exception

  26. 26

    JAX-RS Custom @Provider for a subset of the type

  27. 27

    JAX-RS 2.0 Client - send multipart message with RESTEasy client

  28. 28

    Jersey Client / JAX-RS and optional (not default) @QueryParam (client side)

  29. 29

    Jersey JAX RS error - No provider classes found

HotTag

Archive