bean-validation

Using @Autowired component in custom JSR-303 validator

喜你入骨 提交于 2019-12-04 18:49:27
I'm trying to implement a custom validator for my model classes that autowires a custom bean of mine (declared via @Component ). In this, I followed the Spring documentation on that topic . My AuthenticationFacade object is implemented according to this tutorial . When running my tests, however, the autowired attribute in the Validator object is always null . Why is that? Here are the relevant parts of my code: My custom bean , AuthenticationFacadeImpl.java @Component public class AuthenticationFacadeImpl implements AuthenticationFacade { boolean hasAnyRole(Collection<String> roles) { //

@Valid (jsr 303) not working in Spring mvc 3.0

五迷三道 提交于 2019-12-04 18:45:59
I'm trying to achieve the JSR 303 bean validation in Spring 3.0 by using a simple login use case. The problem is if I submit the form without any value the validation is not happening (i.e the BindingResult method hasErrors() always returning 'false' and printing I'm cool ! . Following is the code snippet: @Controller public class AnnotatedController { @RequestMapping(value = "login") public String validateLogin(@Valid LoginForm loginForm, BindingResult result, HttpServletRequest request) { if(result.hasErrors()) System.out.println("Got Errors !"); else System.out.println("I'm cool !"); return

JSR-303 validation is ignored when custom Validator is annotated with @Component

江枫思渺然 提交于 2019-12-04 18:40:25
I've noticed that JSR-303 validation is completely ignored in Spring when a custom Validator bean annotated with @Component is declared. Interestingly enough said custom validator doesn't even have to filled in or used by any of the classes. The fact that its component scanned by Spring appears to be enough to make Spring skip JSR-303 validation during object binding altogether. Removing @Component from custom Validator and restarting web application enables JSR-303 validation as expected. Annotating custom validators with @Component has its uses eg to have Spring Autowire dependencies.

Convert JSR-303 validation errors to Spring's BindingResult

半腔热情 提交于 2019-12-04 18:26:04
问题 I have the following code in Spring controller: @Autowired private javax.validation.Validator validator; @RequestMapping(value = "/submit", method = RequestMethod.POST) public String submitForm(CustomForm form) { Set<ConstraintViolation<CustomForm>> errors = validator.validate(vustomForm); ... } Is it possible to map errors to Spring's BindingResult object without manually going through all the errors and adding them to the BindingResult ? Something like this: // NOTE: this is imaginary code

Jersey/JAX-RS : How to cascade beans-validation recursively with @Valid automatically?

Deadly 提交于 2019-12-04 15:03:30
问题 I am validating my POJOs in a REST resource endpoint in Jersey: public class Resource { @POST public Response post(@NotNull @Valid final POJO pojo) { ... } } public class POJO { @NotNull protected final String name; @NotNull @Valid protected final POJOInner inner; ... } public class POJOInner { @Min(0) protected final int limit; ... } This seems to work fine. However, the @Min(0) annotation is only verified if the field inner has the @Valid annotation. It doesn't feel right to add the @Valid

@Autowired bean null in ConstraintValidator when invoked by Sessionfactory.getCurrentSession.merge

有些话、适合烂在心里 提交于 2019-12-04 14:40:55
问题 I have just implemented Bean Validation with Hibernate. If I call the validator explicitly it works as expected and my @Autowired DAO bean that connects to the DB is injected as expected. I had previously discovered that I needed to add the statement below before the above would work. I had made extensive use of @Autowired beans before but the statement below was necessary for the validator to be managed by Spring and the bean injected into the ConstraintValidator. <bean id="validator" class=

Why session bean method throw EjbTransactionRolledbackException when RuntimeException was thrown

别等时光非礼了梦想. 提交于 2019-12-04 13:52:15
问题 I am trying to persist the entity with constraint validation, when invoke persist - there is constraint that thrown and the caller get EjbTransactionRolledbackException ... so I try to call the validation explicit and throw ConstraintViolationException / RuntimeException and still the caller get EjbTransactionRolledbackException ... when I throw MyException extends Exception - the caller get MyException Even when I call explicit sc.setRollBackOnly it's still happened :( This shouldn't be the

JSR - 349 bean validation for Spring @RestController with Spring Boot

回眸只為那壹抹淺笑 提交于 2019-12-04 13:42:35
问题 I am using Spring Boot 1.5.2.RELEASE and not able to incorporate JSR - 349 ( bean validation 1.1 ) for @RequestParam & @PathVariable at method itself. For POST requests, if method parameter is a Java POJO then annotating that parameter with @Valid is working fine but annotating @RequestParam & @PathVariable with something like @NotEmpty , @Email not working. I have annotated controller class with Spring's @Validated There are lots of questions on SO and I have commented on this answer that

How to Validate different model class into one form using Spring MVC JSR-303 Validator

Deadly 提交于 2019-12-04 13:11:25
I have form inside jsp page as below: <springForm:form action="${addAction}" name="frm" method="post" commandName="employee"> <table> <tr> <td><label>First Name</label> </td> <td><springForm:input path="firstName" /></td> <td><springForm:errors path="firstName" cssClass="error" /></td> </tr> <tr> <td><label>Last Name</label></td> <td><springForm:input path="lastName" /></td> <td><springForm:errors path="lastName" cssClass="error" /></td> </tr> <tr> <td><label>Email</label> </td> <td><springForm:input path="email" /></td> <td><springForm:errors path="email" cssClass="error" /></td> </tr> <tr>

Bean-Validation 1.1 in Glassfish 4.0 - CDI Injection not working as intended

南笙酒味 提交于 2019-12-04 11:35:42
问题 According to the Glassfish 4.0 wiki, Glassfish 4.0 should include JSR349 Bean Validation 1.1.: GF4 wiki link According to the JSR349 spec, CDI Injection should work out of the box: Bean Validation 1.1. CDI Integration So I changed my pom.xml accordingly: <dependency> <groupId>javax.validation</groupId> <artifactId>validation-api</artifactId> <version>1.1.0.Final</version> <scope>provided</scope> </dependency> And tried injecting a CDI Bean into the ConstraintValidator: public class