java - @Valid annotation is not working in spring boot -
here scenario, controller annotated @restcontroller
, put
method @requestbody
argument needs validated. use @valid annotation on argument , @notnull
,@min
annotations on bean fields, not working.
code here:
the bean:
public class purchasewrapper { @decimalmin(value = "0.00",message = "discount must positive") @notnull private bigdecimal discount; @notnull private long merchandiseid; @notnull private long addressid; @min(1) @notnull private integer count; }
the controller
@restcontroller @requestmapping("merchandises") public class merchandisecontroller { @requestmapping(value = "purchase",method = requestmethod.put) public responseentity<restentity> purchase(@valid @validated @requestbody purchasewrapper purchasewrapper, @requestparam string token){ return new responseentity<>(merchandiseservice.purchase(purchasewrapper,token),httpstatus.ok); } @autowired purchasewrappervalidator purchasewrappervalidator; @initbinder(value = "purchasewrapper") protected void initbinder(webdatabinder binder) { binder.setvalidator(purchasewrappervalidator); } }
the pom file:
<dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-web</artifactid> <dependency> <dependency> <groupid>org.hibernate</groupid> <artifactid>hibernate-validator</artifactid> </dependency>
i have no idea what's wrong here... , guess it's problem use @valid
, @validated
annotations both on same argument. though omit @validated
annotation, @valid
still not working...
any ideas?
i figured out... it's because purchasewrappervalidator
implements org.springframework.validation.validator
overrides default javax.validation.*
annotations.
Comments
Post a Comment