简体   繁体   中英

ExceptionHandler doesn't work in controller

I want to change the return status of REST API request with spring.

I have created an exception class:

public class AutomatedActionValidatorException extends RuntimeException {

    public AutomatedActionValidatorException(String message) {
        super(message);
    }
}

And a controller (commented my original code for testing the exception throwing):

@Controller
@RequestMapping("/api/automated_actions")
public class AutomatedActionController {

    @Resource
    private AutomatedActionsService automatedActionsService;

    @Resource
    private AutomatedActionResponseService automatedActionResponseService;

    @Resource
    private AutomatedActionsSchedulingService automatedActionsSchedulingService;

    @ResponseBody
    @RequestMapping(value = "/save_and_run", method = RequestMethod.POST)
    public AutomatedAction saveAndRunAutomatedAction(HttpServletRequest request,
                                                     @RequestBody AutomatedActionRequest automatedActionRequest) {
        throw new AutomatedActionValidatorException("");

//        AutomatedAction automatedAction = saveAutomatedAction(request, automatedActionRequest);
//        automatedActionsSchedulingService.runAutomatedActions(Collections.singletonList(automatedAction));
//        return automatedAction;
    }

    private AutomatedAction saveAutomatedAction(HttpServletRequest request,
                                               @RequestBody AutomatedActionRequest automatedActionRequest) {
        return automatedActionsService.save(automatedActionRequest);
    }


    @ExceptionHandler(value = AutomatedActionValidatorException.class)
    @ResponseStatus(value = HttpStatus.BAD_REQUEST)
    @ResponseBody
    public String handleAutomatedActionValidatorException(AutomatedActionValidatorException ex) {
        return ex.getMessage();
    }

And I still receiving 500 error code when calling to that endpoint.

From logs that I added, the handleAutomatedActionValidatorException method has ignored.

The response that I'm receiving is:

{
  errorMessage: "Unexpected error occurred.", data: null, success: false, 
  errorMessageKey: null}
  data: null
  errorMessage: "Unexpected error occurred."
  errorMessageKey: null
  success: false
}

Any idea what did I miss?

Thanks

(I am not sure if it would throw status code 500 if your @RequestBody AutomatedActionRequest automatedActionRequest is sent in wrong format by the client, you might want to comment out this code until it throws the exception you are waiting for

Edit: if the RequestBody would be in wrong format it would drop BAD_REQUEST. I your exception 500 must happen before your code would reach at this point)

If you don't do anything special in the ExceptionHandler maybe it would be enough for you to change your exception to this:

@ResponseStatus(code = HttpStatus.BAD_REQUEST, reason = "Bad request!")
public class AutomatedActionValidatorException extends RuntimeException {

    public AutomatedActionValidatorException(String message) {
        super(message);
    }
}

Please check if it is working. (put in comment block your exception handler)

If you get back the code 400, then you will know that the problem is in the exception handler.

These might help you:

https://spring.io/blog/2013/11/01/exception-handling-in-spring-mvc https://www.baeldung.com/spring-response-status-exception

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM