bean-validation

How to set Locale in Bean Validation

十年热恋 提交于 2019-12-03 16:17:58
By default Bean Validation gets Locale based on Locale.getDefault(), which is common to whole JVM. How to change BeanValidation's Locale for current EJB method call? I'm using JavaEE7 and want to get benefits from integration of JPA and Bean Validation, i.e. automatic triggering validation on insert/update/delete events, and as far as possible avoid of writing everything manually. EDIT After all, I'm just returning non-interpolated messages from EJB: public class DoNothingMessageInterpolator implements MessageInterpolator { @Override public String interpolate(String message, Context context) {

How to use SpringMVC @Valid to validate fields in POST and only not null fields in PUT

不羁岁月 提交于 2019-12-03 13:48:11
We are creating a RESTful API with SpringMVC and we have a /products end point where POST can be used to create a new product and PUT to update fields. We are also using javax.validation to validate fields. In POST works fine, but in PUT the user can pass only one field, and I can't use @Valid, so I will need to duplicate all validations made with annotation with java code for PUT. Anyone knows how to extend the @Valid annotation and creating something like @ValidPresents or something else that solve my problem? You can use validation groups with the Spring org.springframework.validation

Spring MVC and JSR-303 hibernate conditional validation

青春壹個敷衍的年華 提交于 2019-12-03 12:38:59
I've a form I want to validate. It contains 2 Address variables. address1 has always to be validated, address2 has to be validated based on some conditions public class MyForm { String name; @Valid Address address1; Address address2; } public class Address { @NotEmpty private String street; } my controller automatically validates and binds my form obj @RequestMapping(...) public ModelAndView edit( @ModelAttribute("form") @Valid MyForm form, BindingResult bindingResult, ...) if(someCondition) { VALIDATE form.address2 USING JSR 303 the problem is that if I use the LocalValidatorFactoryBean

JSR-303 Bean Validation for enum fields

半城伤御伤魂 提交于 2019-12-03 12:13:26
问题 I have a simple bean with enum field public class TestBean{ @Pattern(regexp = "A|B") //does not work private TestEnum testField; //getters + setters } enum TestEnum{ A, B, C, D } I would like to validate testField using Bean Validation. Specifically I would like to make sure that only A and B values are allowed (for a particular calidation gropus). It seems that enums are not handled JSR 303 (I was trying to use @Pattern validator) or I am doing something in a wrong way. I am getting

Convert JSR-303 validation errors to Spring's BindingResult

↘锁芯ラ 提交于 2019-12-03 12:02:14
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 BindingResult bindingResult = BindingResult.fromConstraintViolations(errors); I now it is possible to

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

不想你离开。 提交于 2019-12-03 10:23:33
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 annotation to each field which isn't a primitive. Is there a way to tell the bean validator to

JPA - No validator could be found for type: enum

末鹿安然 提交于 2019-12-03 09:44:10
I have an entity class which uses an enum type for one of the properties, and I am getting the following exception when I try to persist the entity: javax.validation.UnexpectedTypeException: No validator could be found for type: model.schema.BaseYesNo Any idea why this might be happening? My thinking is that since it is an enum, it should already be validated by the compiler, so no need for some kind of validator. (code below) The entity property: @Enumerated( EnumType.STRING ) @Column( name = "seeded_flag" ) private BaseYesNo seededFlag; public BaseYesNo getSeededFlag() { return this

Why session bean method throw EjbTransactionRolledbackException when RuntimeException was thrown

僤鯓⒐⒋嵵緔 提交于 2019-12-03 08:20:18
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 behavior. what's going on? Configuration: Netbeans 6.9.1 Glassfish 3.0.1 JPA 2.0 (EclipseLink) EJB 3.1

Spring Boot JSR-303/349 configuration

梦想的初衷 提交于 2019-12-03 08:11:29
In my Spring Boot 1.5.1 application I'm trying to configure support of JSR-303 / JSR-349 validation. I have added a following annotations @NotNull @Size(min = 1) to my method: @Service @Transactional public class DecisionDaoImpl extends BaseDao implements DecisionDao { @Override public Decision create(@NotNull @Size(min = 1) String name, String description, String url, String imageUrl, Decision parentDecision, Tenant tenant, User user) { ... } } I'm trying to invoke this method from my test, but it does not fail on the validation constraints. This is my test and configs: @SpringBootTest

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

。_饼干妹妹 提交于 2019-12-03 07:46:54
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 its not working for me. Spring Boot includes - validation-api-1.1.0.Final.jar and hibernate-validator-5.3