spring-validator

Add value into request object before validation

浪尽此生 提交于 2019-12-11 04:14:57
问题 I'm using @Restcontroller and @Vaid annotation. @RequestMapping(path = "/1050" method = RequestMethod.POST, headers = {"Content-Type=application/json"}, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE ) public UserListResp getUserList(@RequestBody @Valid UserListReq request, BindingResult bindingResult, Principal principal){ UserListResp response = new UserListResp(); if (bindingResult.hasErrors()){ response.setResultCode(102); // Validation

Spring mvc not able to read messages.properties file

為{幸葍}努か 提交于 2019-12-10 12:02:27
问题 I am trying to use custom validation error messages by using properties file. I have placed messages.properties file in web content/web-inf/ folder. NonEmpty.batchDomain.batchName=Invalid message 2. My applicationContext file is : <context:component-scan base-package="com.coaching.controller" /> <!-- Enable annotation driven controllers, validation etc... --> <mvc:annotation-driven /> <!-- Resolves view names to protected .jsp resources within the /WEB-INF/views directory --> <bean id=

How to validate Mono when using Spring Reactive

南楼画角 提交于 2019-12-08 03:25:45
问题 We are evaluating Spring 5 for a project, and not sure how best to validate Mono parameters. Traditionally we had been using MethodValidationPostProcessor to validate our method parameters as below: @Validated @Service public class FooService @Validated(SignUpValidation.class) public void signup(@Valid UserCommand userCommand) { ... } We would then handle the exception in a ControllerAdvice or ErrorController , and pass a suitable 4xx response to the client. But when I change the parameter to

javax.validation.constraints.Email matching invalid email address

∥☆過路亽.° 提交于 2019-12-07 05:38:52
问题 I have a User entity having email property annotated with @Email @Email private String email; I am using @Valid (javax.validation.Valid) annotation on my Controller class. The issue is that the controller validator is passing the invalid emails. Example: pusp@1 - obviously this is an invalid email address pusp@fake The pattern I noticed is, the @Email only want sometext@text , it don't care for the extensions(.com/org etc). Is it the expected behaviour? Do I need to pass my own regex

How to validate Mono when using Spring Reactive

a 夏天 提交于 2019-12-06 12:19:30
We are evaluating Spring 5 for a project, and not sure how best to validate Mono parameters. Traditionally we had been using MethodValidationPostProcessor to validate our method parameters as below: @Validated @Service public class FooService @Validated(SignUpValidation.class) public void signup(@Valid UserCommand userCommand) { ... } We would then handle the exception in a ControllerAdvice or ErrorController , and pass a suitable 4xx response to the client. But when I change the parameter to Mono , as below, it no more seems to work. @Validated @Service public class FooService @Validated

Spring Validation - Class level validation to address field errors

好久不见. 提交于 2019-12-05 18:30:38
If we define a class level validation annotation, such as one which compare fields and have a ConstraintValidator like this: public class ComparisonValidator implements ConstraintValidator<ValueMatches, Object> { private String[] fields; @Override public void initialize(final ValueMatches constraintAnnotation) { fields = constraintAnnotation.fields(); } @Override public boolean isValid(final Object value, final ConstraintValidatorContext context) { if (fields.length == 0) { return true; } final BeanWrapperImpl beanWrapper = new BeanWrapperImpl(value); final Object comparisonValue = beanWrapper

javax.validation.constraints.Email matching invalid email address

时光怂恿深爱的人放手 提交于 2019-12-05 09:40:42
I have a User entity having email property annotated with @Email @Email private String email; I am using @Valid (javax.validation.Valid) annotation on my Controller class. The issue is that the controller validator is passing the invalid emails. Example: pusp@1 - obviously this is an invalid email address pusp@fake The pattern I noticed is, the @Email only want sometext@text , it don't care for the extensions(.com/org etc). Is it the expected behaviour? Do I need to pass my own regex implementation for @Email(regex="") A email without . may be considered as valid according to the validators.

Spring Validate List of Strings for non empty elements

做~自己de王妃 提交于 2019-12-04 18:53:01
问题 I have a model class which has list of Strings. The list can either be empty or have elements in it. If it has elements, those elements can not be empty. For an example suppose I have a class called QuestionPaper which has a list of questionIds each of which is a string. class QuestionPaper{ private List<String> questionIds; .... } The paper can have zero or more questions. But if it has questions, the id values can not be empty strings. I am writing a micro service using SpringBoot,

What does this nested annotation do / allow?

自作多情 提交于 2019-12-04 02:56:53
I was looking at the @org.hibernate.validator.constaints.NotEmpty annotation: @Documented @Constraint(validatedBy = { }) @Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER }) @Retention(RUNTIME) @ReportAsSingleViolation @NotNull @Size(min = 1) public @interface NotEmpty { String message() default "{org.hibernate.validator.constraints.NotEmpty.message}"; Class<?>[] groups() default { }; Class<? extends Payload>[] payload() default { }; /** * Defines several {@code @NotEmpty} annotations on the same element. */ @Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER })

How to: Spring get rid of @Validate for automatic Controller validation?

℡╲_俬逩灬. 提交于 2019-12-03 16:12:10
I know about @Valid annotation to instruct spring to validate for example a Controller argument according to JSR-303 in such this example: @GetMapping("/test") public TestDTO testDTO(@Valid TestDTO testDTO){ return testDTO; } But I would like to be able to configure Spring in some way to enable validation in all my controllers without specify explicitly the @Valid annotation. Is that possible in any way? Some Spring configuration? Making use of AOP?... I have finally came across with a working solution which may be not the optimal from the point of view of Spring configuration (as I said I'm