提交表单时,字段有的没有值,springboot 会报错。
org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 4 errors
Field error in object 'stone' on field 'stone_purchdate': rejected value [2018-05-11T16:00:00.000+0000]; codes [typeMismatch.stone.stone_purchdate,typeMismatch.stone_purchdate,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [stone.stone_purchdate,stone_purchdate]; arguments []; default message [stone_purchdate]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'stone_purchdate'; nested exception is java.lang.IllegalArgumentException: Could not parse date: Unparseable date: "2018-05-11T16:00:00.000+0000"]
Field error in object 'stone' on field 'stone_substoPramt': rejected value []; codes [typeMismatch.stone.stone_substoPramt,typeMismatch.stone_substoPramt,typeMismatch.double,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [stone.stone_substoPramt,stone_substoPramt]; arguments []; default message [stone_substoPramt]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'double' for property 'stone_substoPramt'; nested exception is java.lang.NumberFormatException: empty String]
Field error in object 'stone' on field 'stone_substoPrperct': rejected value []; codes [typeMismatch.stone.stone_substoPrperct,typeMismatch.stone_substoPrperct,typeMismatch.double,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [stone.stone_substoPrperct,stone_substoPrperct]; arguments []; default message [stone_substoPrperct]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'double' for property 'stone_substoPrperct'; nested exception is java.lang.NumberFormatException: empty String]
Field error in object 'stone' on field 'stone_substoWgt': rejected value []; codes [typeMismatch.stone.stone_substoWgt,typeMismatch.stone_substoWgt,typeMismatch.double,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [stone.stone_substoWgt,stone_substoWgt]; arguments []; default message [stone_substoWgt]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'double' for property 'stone_substoWgt'; nested exception is java.lang.NumberFormatException: empty String]
at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.resolveArgument(ModelAttributeMethodProcessor.java:157) ~[spring-web-5.0.6.RELEASE.jar:5.0.6.RELEASE]
at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:124) ~[spring-web-5.0.6.RELEASE.jar:5.0.6.RELEASE]
at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:161) [spring-web-5.0.6.RELEASE.jar:5.0.6.RELEASE]
解决方法:
参数里加上 BindingResult 即可
General interface that represents binding results. Extends the interface
for error registration capabilities, allowing for a Validator
to be applied, and adds binding-specific analysis and model building.
Serves as result holder for a DataBinder
,
obtained via the DataBinder.getBindingResult()
method. BindingResult implementations can also be used directly, for example to
invoke a Validator
on it (e.g. as part of a unit test).
- Since:
- 2.0
- Author:
- Juergen Hoeller
- See Also:
- DataBinder
- Errors
- Validator
- BeanPropertyBindingResult
- DirectFieldBindingResult
- MapBindingResult