java - 如何在 Spring MVC 中更改 BindingResult 中的异常消息

标签 java spring-boot spring-mvc

我是 Spring 的新生儿。我尝试做CRUD。有一个模型

public class Customer {
    @NotNull
    private String id;
    @Size(min=4,max=50)
    private String name;
    @Size(min=4,max=50)
    private String address;
    @NotNull
    @Past
    private Date birthDay;
    private boolean noOfOrdersMade;
    private Gender gender;

.... 和 Controller

@RequestMapping(value="/add",method=RequestMethod.GET)
    public String getNewCustomerForm(Model model) {
        Customer customer = new Customer();
        model.addAttribute("newCustomer",customer);
        return "customerAdd";
    }
    @RequestMapping(value="/add",method=RequestMethod.POST)
    public String proceccNewCustomerForm(@ModelAttribute("newCustomer") @Valid Customer customer, BindingResult bindingResult,Model model) {
        if(bindingResult.hasErrors()) {
            return "customerAdd";
        }
        customerService.addCustomer(customer);
        return "redirect:/customers/";
    }

和 DateDormater 用于在 WebApplicationContextConfig 中本地化格式输入日期

public void addFormatters (FormatterRegistry registry) {
        registry.addFormatter(new DateFormatter());
}

当我以错误的格式输入birthDay时,我收到异常消息

"Failed to convert property value of type java.lang.String to required type java.util.Date for property birthDay; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@javax.validation.constraints.NotNull @javax.validation.constraints.Past @com.packt.webstore.validator.ValidDate java.util.Date] for value dsfsadf; nested exception is java.lang.IllegalArgumentException: Parse attempt failed for value [dsfsadf]"

What is on my Add Form

在我的控制台日志中我得到:

Field error in object 'newCustomer' on field 'birthDay': rejected value [dfsf]; codes [typeMismatch.newCustomer.birthDay,typeMismatch.birthDay,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [newCustomer.birthDay,birthDay]; arguments []; default message [birthDay]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'birthDay'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@javax.validation.constraints.NotNull @javax.validation.constraints.Past @com.packt.webstore.validator.ValidDate java.util.Date] for value 'dfsf'; nested exception is java.lang.IllegalArgumentException: Parse attempt failed for value [dfsf]]
Field error in object 'newCustomer' on field 'address': rejected value [AAA]; codes [Size.newCustomer.address,Size.address,Size.java.lang.String,Size]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [newCustomer.address,address]; arguments []; default message [address],50,4]; default message [size must be between 4 and 50]}

第一行有一条错误消息,我想对其进行自定义。 第二行是标准@Size(min=4,max=50)验证者的消息。

如何将此消息更改为本地化的、用户友好的当前区域语言。

我发现Spring JDBC有异常翻译器https://www.baeldung.com/spring-jdbc-jdbctemplate 。但这不是我的情况。

我在StackOverfolw上发现了类似的问题,但它无法解决我的问题。 我希望有人能帮助我。

最佳答案

你可以使用3种方式来处理异常: 1. 使用这个SimpleMappingExceptionResolver类 2. 实现HandlerExceptionResolver接口(interface) 3.使用@ExceptionHandler注解

关于java - 如何在 Spring MVC 中更改 BindingResult 中的异常消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59210142/

相关文章:

java - 将任何文件类型解析为 XML

java - Derby 嵌入式数据库中的查询

spring-boot - 如何安装curl来进行spring boot jib docker镜像的健康检查?

java - Kafka无序消息场景处理

java - spring中根据请求体将相同的url映射到不同的方法

java - 维护两个数组列表的推送和打印值的顺序

java - Spring Boot 处理 get 请求中的多个参数

java - 基于 Spring 的 Web 应用程序的环境特定配置?

spring-boot - 使用 Websocket 的单个实例上的 RabbitMQ 最大连接数

java - Android 支持库问题 23.2.1 AppBarLayout setExpanded