1

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!!

1 Answer 1

0

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.