Search code examples
spring-mvcspring-restcontroller

JSON support in Global Exception Handler Spring MVC Rest controller


I am creating a global exception handler in spring mvc application with rest controller.

From controller when I am throwing Exception, the global exception handler catches it, but it is not returning the json data. But when I am using the @ExceptionHandler in the controller, it returns json data.

controller code:

import javax.validation.Valid;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.sphiextest1back.bean.NewUserBean;
import com.sphiextest1back.bean.ResponseBean;
import com.sphiextest1back.exception.FieldValidationException;

@RestController
@RequestMapping(value = "/service")
public class User {

    @RequestMapping(value = "/test/test", method = RequestMethod.GET)
    public ResponseBean testFunc(@Valid NewUserBean newUserBean, BindingResult result) throws FieldValidationException {
        ResponseBean responseBean = new ResponseBean();
        if(result.hasErrors()) {
            throw new FieldValidationException("Error in field values", responseBean);
        }
        else {
            responseBean.setMessage("Test Message1");
            responseBean.setSuccess(true);
            responseBean.setStatusCode(200);
        }


        return responseBean;
    }

    /**This Works fine when uncommented**/
    /*@ExceptionHandler(FieldValidationException.class)
    @ResponseBody
    public ResponseBean handleFieldValidationException(Exception ex) {

        ResponseBean responseBean = new ResponseBean();
        responseBean.setStatusCode(401);
        responseBean.setMessage(ex.getMessage());
        responseBean.setSuccess(false);
        return responseBean;

    }*/

}

But when using global exception handler, it is not returning json data rather showing HTTP error page with status 500.

Here is my global exception handler code.

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import com.sphiextest1back.bean.ResponseBean;

@ControllerAdvice
public class GlobalExceptionHandler {

    /**
     * Method handles the FieldValidationException
     * 
     * @param ex
     * @return responseBean
     */
    @ExceptionHandler(FieldValidationException.class)
    public @ResponseBody ResponseBean handleFieldValidationException(Exception ex) {

        ResponseBean responseBean = new ResponseBean();
        responseBean.setStatusCode(401);
        responseBean.setMessage(ex.getMessage());
        responseBean.setSuccess(false);
        return responseBean;

    }
}

Is there any way to return json data from ResponseBean object!!


Solution

  • It was my mistake. In the xml file I did not include the global exception package for

    Now I included the package and it is working fine.