简体   繁体   English

ExceptionHandler 在控制器中不起作用

[英]ExceptionHandler doesn't work in controller

I want to change the return status of REST API request with spring.我想用 spring 更改 REST API 请求的返回状态。

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.调用该端点时,我仍然收到 500 错误代码。

From logs that I added, the handleAutomatedActionValidatorException method has ignored.从我添加的日志中, handleAutomatedActionValidatorException方法已被忽略。

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 (我不确定如果客户端以错误的格式发送您的@RequestBody AutomatedActionRequest automatedActionRequest请求,它是否会抛出状态代码 500,您可能想要注释掉此代码,直到它抛出您正在等待的异常

Edit: if the RequestBody would be in wrong format it would drop BAD_REQUEST.编辑:如果 RequestBody 格式错误,它将丢弃 BAD_REQUEST。 I your exception 500 must happen before your code would reach at this point)我您的异常 500 必须在您的代码到达此时之前发生)

If you don't do anything special in the ExceptionHandler maybe it would be enough for you to change your exception to this:如果您没有在 ExceptionHandler 中做任何特别的事情,也许将您的异常更改为这样就足够了:

@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.如果您返回代码 400,那么您就会知道问题出在异常处理程序中。

These might help you:这些可能对你有帮助:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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