bean-validation

Hibernate Validator method or constructor validation

房东的猫 提交于 2019-12-01 12:00:58
How can I use Hibernate validator to validate the arguments inside the constructor or method? I want the validation to occur before the ValueObject creation so I can throw an exception and not create the object unless all parameters are valid. Basically I'm trying to use annotations instead of doing something like this if possible: public class ConditionalPerson { private String name; private String surname; private int age; public ConditionalPerson(String name, String surname, int age){ if (name == null || surname == null || age < 1) { throw new IllegalArgumentException(); } this.name = name;

JSR 303. Validate method parameter and throw exception

我的梦境 提交于 2019-12-01 11:58:57
问题 How can I use JSR-303 validate method parameters and throw exception if parameter is not valid? For example like this: public void createUser(@ValidOrThrowException User user) {...} ? For now, I check every method parameter in method body, like public void createUser(User user) { ConstraintViolations violations = Validator.validate(user); if (!violations.isEmpty()) { throw new IllegalArgumentException(createExceptionMessage(violations )); } ...//business logic } and I think it's ugly. P.S. As

Hibernate Validator method or constructor validation

≡放荡痞女 提交于 2019-12-01 10:52:17
问题 How can I use Hibernate validator to validate the arguments inside the constructor or method? I want the validation to occur before the ValueObject creation so I can throw an exception and not create the object unless all parameters are valid. Basically I'm trying to use annotations instead of doing something like this if possible: public class ConditionalPerson { private String name; private String surname; private int age; public ConditionalPerson(String name, String surname, int age){ if

How to obtain ConstraintValidatorContext?

余生长醉 提交于 2019-12-01 10:26:51
I am writing code that has explicit call to Bean Validation (JSR-303) something like this: public class Example { @DecimalMin(value = "0") private static final String ANNOTATED = ""; public void isPossitiveNumber(String str){ ValidatorFactory factory = Validation.buildDefaultValidatorFactory(); ConstraintValidator<DecimalMin, String> validator = factory.getConstraintValidatorFactory().getInstance( DecimalMinValidatorForString.class); validator.initialize( ReflectionUtils.findField(getClass(), "ANNOTATED") .getAnnotation( DecimalMin.class)); boolean isValid = validator.isValid(str, null);

The JSR 303 bean validation, The extended ConstraintValidator cannot use the CDI

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-01 09:28:55
问题 I've tried to learn the JSF 2.0 with bean validation at the class level as the following: - The utility @Singleton public class MyUtility { public boolean isValid(final String input) { return (input != null) || (!input.trim().equals("")); } } The constraint annotation @Retention(RetentionPolicy.RUNTIME) @Target({ ElementType.TYPE, ElementType.ANNOTATION_TYPE, ElementType.FIELD }) @Constraint(validatedBy = Validator.class) @Documented public @interface Validatable { String message() default

Validate only if the field is not Null

百般思念 提交于 2019-12-01 08:24:12
I use JSR303 Spring Validation and I have the following; @Digits(fraction = 0, integer = 15) private String tpMobile; Validation says Must be number between 10-15 digits , but the thing is this field is optional . So when user left it empty also the same error message shows. So what I want to do is validate only if the field is not empty. If the field is empty skip validating that field Instead of using @Digit , I would like to suggest you to use @Pattern(message="YOUR_MESSAGE", regexp="REGULAR_EXPRESSION") Main reason is that we show message for such set of values for which we want to

How to obtain ConstraintValidatorContext?

旧时模样 提交于 2019-12-01 06:58:58
问题 I am writing code that has explicit call to Bean Validation (JSR-303) something like this: public class Example { @DecimalMin(value = "0") private static final String ANNOTATED = ""; public void isPossitiveNumber(String str){ ValidatorFactory factory = Validation.buildDefaultValidatorFactory(); ConstraintValidator<DecimalMin, String> validator = factory.getConstraintValidatorFactory().getInstance( DecimalMinValidatorForString.class); validator.initialize( ReflectionUtils.findField(getClass(),

Customizing JAX-RS response when a ConstraintViolationException is thrown by Bean Validation

和自甴很熟 提交于 2019-12-01 06:19:06
Bean Validation is a good option to validate objects, but how to customize the response of a REST API (using RESTeasy) when a ConstraintViolationException is thrown? For example: @POST @Path("company") @Consumes("application/json") public void saveCompany(@Valid Company company) { ... } A request with invalid data will return a HTTP 400 status code with the following body: [PARAMETER] [saveCompany.arg0.name] [{company.name.size}] [a] It's nice but not enough, I would like to normalize these kind of errors in a JSON document. How can I customize this behavior? With JAX-RS can define an

Validate only if the field is not Null

廉价感情. 提交于 2019-12-01 06:06:46
问题 I use JSR303 Spring Validation and I have the following; @Digits(fraction = 0, integer = 15) private String tpMobile; Validation says Must be number between 10-15 digits , but the thing is this field is optional . So when user left it empty also the same error message shows. So what I want to do is validate only if the field is not empty. If the field is empty skip validating that field 回答1: Instead of using @Digit , I would like to suggest you to use @Pattern(message="YOUR_MESSAGE", regexp=

Jersey/JAX-RS resource method input bean validation

送分小仙女□ 提交于 2019-12-01 05:18:54
I am using Jersey/JAX-RS via DropWizard 0.7.1 to expose RESTful service endpoints. I have all of my entity POJOs annotated with both JAX-RS and Hibernate/JSR-303 bean validation annotations like so: public class Widget { @JsonProperty("fizz") @NotNull @NotEmpty private String fizz; // Can't be empty or null @JsonProperty("buzz") @Min(value=5L) private Long buzz; // Can't be less than 5 // etc. } When a resource method receives one of these POJOs as input (under the hood, DropWizard has already deserialized the HTTP entity JSON into a Widget instance), I would like to validate it against the