Nitin
Nitin

Reputation: 57

Unable to receive response data from @ExceptionHalndler method

I have deployed Restful Web Serivce on the server and accessing it from Client application.

I am using JSON to communicate data between services & client.

Normal flow is working perfect.

But,in case of Exception, I am able to receive HTTP STATUS & reason for Exception but not receiving Response Body.

Here is my Exception Handler

@ExceptionHandler(MyException.class)
@ResponseStatus(value=HttpStatus.INTERNAL_SERVER_ERROR,reason="Unable to fetch data")      

public @ResponseBody String handle(MyException ex)

    {
        ErrorInfo error = new ErrorInfo();   
        String response = null;
        error.setErrorStatus(String.valueOf(HttpStatus.INTERNAL_SERVER_ERROR));  
        error.setErrorCode(String.valueOf(HttpStatus.INTERNAL_SERVER_ERROR));   // TODO Need to store Application specific error code
        error.setErrorMessage("Communication Error : Unable to fetch conference data");                  // TODO Need to get from message resource file
        error.setDeveloperMessage(ex.getLocalizedMessage());


        response = utilities.fromBeanToJson(error);
        System.out.println("***Response : " + response);
        return response;   // Not able to receive this info
    }

Actual Response :

<html><head><title>Apache Tomcat/6.0.29 - Error report</title><style><!--H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}A.name {color : black;}HR {color : #525D76;}--></style> </head><body><h1>HTTP Status 500 - Unable to fetch conference data</h1><HR size="1" noshade="noshade"><p><b>type</b> Status report</p><p><b>message</b> <u>Unable to fetch data</u></p><p><b>description</b> <u>The server encountered an internal error (Unable to fetch data) that prevented it from fulfilling this request.</u></p><HR size="1" noshade="noshade"><h3>Apache Tomcat/6.0.29</h3></body></html>

Upvotes: 1

Views: 91

Answers (1)

geoand
geoand

Reputation: 64069

You need to remove the reason from @ResponseStatus. This is needed because from the the Javadoc of @ResponseStatus we can see:

The reason to be used for the response.

If this element is not set, it will default to the standard status message for the status code. Note that due to the use of {@code HttpServletResponse.sendError(int, String)}, the response will be considered complete and should not be written to any further.

Upvotes: 2

Related Questions