简体   繁体   English

Spring MVC验证错误消息

[英]Spring MVC Validation error message

I have a problem with the validation in spring MVC. 我在Spring MVC中验证存在问题。 If I send an invalid request for the body, the application returns to the bad request but not my custom message. 如果我发送的请求无效,则应用程序将返回到错误的请求,但不会返回我的自定义消息。

The code is : 代码是:

public @ResponseBody ResponseEntity<BaseModel> newTicket(@Valid @RequestBody Ticket ticket, Locale locale, BindingResult bindingResult ){
    if(bindingResult.hasErrors()) {
        logger.info("error");
        throw new BadRequestException("Bad Request");
    }

if I try to put the application in debug mode, the if is never executed. 如果我尝试将应用程序置于调试模式,则永远不会执行if。

Can someone help me? 有人能帮我吗?

You can direct the bad request error in the following way: 您可以通过以下方式指示错误的请求错误:

@ExceptionHandler
@ResponseStatus(HttpStatus.BAD_REQUEST)
public void handle(HttpMessageNotReadableException e) {
    //do something here in case of bad request.
}

You can go on and customize according to your needs. 您可以根据需要继续进行自定义。

As you are throwing throw new BadRequestException("Bad Request"); 在抛出时,抛出新的BadRequestException(“ Bad Request”); so it is not displaying your custom message , Instead of throwing this return the same page which you taking input. 因此它不会显示您的自定义消息,而不是抛出此消息,而是返回您输入内容的页面。

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

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