lundi 20 juillet 2015

Java Rest Service Jersey User Defined Exception Handling

I have Created Java Rest service using Jersey Implementation. My Requirement is to throw UserDefined Exception in my Rest Service I have created Exception class and ExceptionMapper Classes

Here is my Java code:

public class AppServerException extends Exception {

    private String errorMessage;
    private int errorId;
    private static final long serialVersionUID = 1L;

    // Parameterless Constructor
    public IT360AppServerException() {
    }

    // Constructor that accepts a message
    public IT360AppServerException(String message, int errorid) {
        super(message);
        errorMessage = message;
        errorId = errorid;

    }

    public String getErrorMessage() {
        return errorMessage;
    }

    public int getErrorId() {
        return errorId;
    }

}

ExceptionMapper Class:

         @Provider
        public class AppServerExceptionMapper implements
                ExceptionMapper<AppServerException> {

            @Override
            public Response toResponse(AppServerException exception) {
                System.out.println("toResponse>>");
                System.out.println("Error Code >>>>"+exception.getErrorId());////printing
                System.out.println("Error Message >>>"+exception.getErrorMessage());//printing

                ErrorResponse errorResponse = new ErrorResponse();
                errorResponse.setErrorCode(exception.getErrorMessage());
                errorResponse.setErrorId(exception.getErrorId());
                return Response.status(Response.Status.INTERNAL_SERVER_ERROR)
                        .entity(errorResponse).type(MediaType.APPLICATION_JSON).build();
            }

        }

Problem: I am able to print the logs inside toResponse() method but the response is not returning.Flow is stopping at the logs itself.Could any one help

Aucun commentaire:

Enregistrer un commentaire