Java 如何更改异常';绑定中的消息将导致Spring MVC
我是春天的新生儿。我试着做积垢。有一个模型Java 如何更改异常';绑定中的消息将导致Spring MVC,java,spring-boot,spring-mvc,Java,Spring Boot,Spring Mvc,我是春天的新生儿。我试着做积垢。有一个模型 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 boole
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;
。。。。
和控制器
@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());
}
当我以错误的格式输入生日时,我会收到异常消息
"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]"
在我的控制台日志中,我得到:
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]}
第一行有一条错误消息,我想对其进行自定义。
第二行是standart@Size(最小=4,最大=50)验证程序的消息
如何将此消息更改为当前地区语言的本地化、用户法语。
我发现SpringJDBC有一个异常转换器。但这不是我的情况
我在StackOverfolw上发现了类似的问题,但它不能解决我的问题。
我希望有人能帮助我。您可以使用3种方法来处理异常: 1.使用此
SimpleMappingExceptionResolver
类
2.实现HandlerExceptionResolver
接口
3.使用
@ExceptionHandler
annotationBenson,谢谢您的回答。我试着按照你的建议去做,但在我看来,这不是我的情况。我看不出有什么例外。它看起来像验证器的消息。我稍微修改了我的问题,并从控制台日志中添加了字符串。我明白了,这是使用JavaSript限制用户输入的最佳方法,但我希望找到不使用JavaScript的解决方案