bean-validation

Injecting Spring Dependencies into ConstrantValidator

天涯浪子 提交于 2021-02-05 12:22:39
问题 I'm using Bean Validation. I have a custom validator @MyValidator that needs to look up a value with an injected Spring managed DAO object. How can I get access to this? Spring isn't injecting the DAO into my "MyValidator" object. @Component public class CodeListValidator implements ConstraintValidator<CodeList, String> { @Autowired private ICodeListCommonService codeListCommonService; private CodeListEnum codeListID; @Override public void initialize(CodeList constraintAnnotation) { this

How to return a custom response pojo when request body fails validations that are defined using Bean Validation/Hibernate Validator?

馋奶兔 提交于 2021-02-04 18:59:28
问题 Is it possible to override the default response POJO of Spring Hibernate validator? Currently when a validation gets failed then a very big response returned to the client as shown below. But I don't want the client to provide the full error response of hibernate validator and instead to send some key-value pair regarding the error message. { "timestamp": "2018-05-28T18:12:56.705+0000", "status": 400, "error": "Bad Request", "errors": [ { "codes": [ "NotBlank.abc.xyz", "NotBlank.xyz",

Are annotations on a type parameter accessible in runtime?

时光毁灭记忆、已成空白 提交于 2021-02-04 14:10:42
问题 Java 8 allows things like: public List<@NonNull String> names; But is there a way to access this annotation in runtime or is it available only to compiler plugins? There's new Method#getAnnotatedReturnType that provides access to annotations on the return type, so I was hoping ParameterizedType would now have something like getActualAnnotatedTypeArguments that would do the same for generic type arguments, but it doesn't exist... 回答1: The new API continues the tradition of requiring lots of

Usage of @Valid vs @Validated in Spring

巧了我就是萌 提交于 2021-01-29 02:28:52
问题 Thanks to this question, I think I understand how validation is working. As for JPA, here we have a specification called JSR-303 explaining how Bean Validation should work and then we are using implementations like the commonly used Hibernate Validator or Apache BVal also. I am struggling about using @Valid in some part of my code. I am not using @Validated because I do not need group validation. You can find an example of a demo project here In PropertyExample class, you can see I marked my

Android and Hibernate Validator - it is possible to use together?

醉酒当歌 提交于 2021-01-28 00:03:51
问题 I wonder if it is possible to use Hibernate Validator on Android. I tried but it looks like some javax packages are missing on Android platform (javax.xml.stream.XMLInputFactory). Here is my code, dependency and error: ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory(); Validator validator = validatorFactory.getValidator(); compile 'org.hibernate:hibernate-validator:5.1.3.Final' compile 'javax.el:javax.el-api:2.2.4' compile 'org.glassfish.web:javax.el:2.2.4' 03-28

Building Dynamic ConstraintViolation Error Messages

£可爱£侵袭症+ 提交于 2020-12-28 20:53:38
问题 I've written a validation annotation implemented by a custom ConstraintValidator . I also want to generate very specific ConstraintViolation objects that use values computed during the validation process during message interpolation. public class CustomValidator implements ConstraintValidator<CustomAnnotation, ValidatedType> { ... @Override public boolean isValid(ValidatedType value, ConstraintValidatorContext context) { // Figure out that the value is not valid. // Now, I want to add a

Bean validation on associations not working when using generation strategy IDENTITY

ⅰ亾dé卋堺 提交于 2020-08-10 19:14:04
问题 I have got the following Parent, Child and Subchild entities: @Entity public class Parent { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "ID", nullable = false, precision = 20) private Long id; @Valid @BatchSize(size = 5) @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, mappedBy = "parent") private List<Child> childs = new ArrayList<>(); ... } @Entity public class Child { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "ID", nullable

How to test a Validator which implements ConstraintValidator in java?

冷暖自知 提交于 2020-08-04 02:36:13
问题 I have an "AllowedValuesValidator.java" class: public class AllowedValuesValidator implements ConstraintValidator<AllowedValues, String> { String[] values; String defaultValue; @Override public void initialize(AllowedValues constraintAnnotation) { values = constraintAnnotation.allowedValues(); defaultValue = constraintAnnotation.defaultValue(); } @Override public boolean isValid(String value, ConstraintValidatorContext context) { if (!StringUtils.isEmpty(defaultValue) && StringUtils.isEmpty