bean-validation

Spring boot, how to use @Valid with List<T>

∥☆過路亽.° 提交于 2019-12-04 02:35:59
I am trying to put validation to a Spring Boot project. So I put @NotNull annotation to Entity fields. In controller I check it like this: @RequestMapping(value="", method = RequestMethod.POST) public DataResponse add(@RequestBody @Valid Status status, BindingResult bindingResult) { if(bindingResult.hasErrors()) { return new DataResponse(false, bindingResult.toString()); } statusService.add(status); return new DataResponse(true, ""); } This works. But when I make it with input List<Status> statuses , it doesn't work. @RequestMapping(value="/bulk", method = RequestMethod.POST) public List

Method Parameter Validation with JSR-303

做~自己de王妃 提交于 2019-12-04 02:19:21
Is JSR-303 also intended for method parameter validation? If so, is there any example on the web? The biggest challenge I'm facing is how to get a validator within each method. With Spring 3, doesn't it mean that I'd have to inject virtually every class with a LocalValidatorFactoryBean ? Thanks! This sounds like a use case for AOP (AspectJ). Write a pointcut for methods that are annotated with javax.validation.constraints.* , inject a validator into the aspect (probably using Spring) and use a @Before or @Around advice to perform the validation before method execution. Read AspectJ in Action

JSR-303 Bean Validation annotate multiple fields

白昼怎懂夜的黑 提交于 2019-12-03 23:07:29
问题 I've just started testing JSR-303 Bean validation and was wondering if something was possible. We have a default regular expression in our app for all String fields. If I want to apply this using bean validation I think I need to annotate each field within my form object. @Pattern(regexp = REG_EXP) private String aString; @Pattern(regexp = REG_EXP) private String anotherString; Is it possible to apply the @Pattern to all Strings (or certain fields) in one hit? We're using the Hibernate

How to change the Validation Error behaviour for Dropwizard?

三世轮回 提交于 2019-12-03 22:15:05
In Dropwizard I use @Valid annotations for my resource methods: public class Address { @NotNull String street ... } @Path("/address") @Produces(MediaType.APPLICATION_JSON) public class AddressResource { @POST public MyResponse addAddress(@Valid Address address) { if (address == null) { throw new WebApplicationException("address was null"); } ... } } On application start I register a custom WebApplicationExceptionMapper which handles WebApplicationExceptions . Thus, for addresses with the value null, the exception is thrown and handled in the mapper which generates a useful response. However,

How to solve NoSuchMethodError for javax.validation when deploying a spring boot application in weblogic server?

拜拜、爱过 提交于 2019-12-03 21:14:20
I am trying to deploy a simple spring boot application that will expose some rest api and I use hibernate entity manager to manipulate entity objects. When I try to deploy this application to Oracle Weblogic 12c, I get the following exception: <Jan 11, 2016 6:20:14 PM BDT> <Error> <Console> <BEA-240003> <Administration Console encountered the following error: weblogic.application.ModuleException: java.lang.NoSuchMethodError: javax.validation.spi.ConfigurationState.getParameterNameProvider()Ljavax/validation/ParameterNameProvider; at weblogic.application.internal.ExtensibleModuleWrapper.start

JSR 303 Bean Validation in Spring

 ̄綄美尐妖づ 提交于 2019-12-03 20:25:35
here is my formbean:- public class LoginFormBean { @NotEmpty private String userId; @NotEmpty private String password; public String getUserId() { return userId; } public void setUserId(String userId) { this.userId = userId; } public String getPassword() { return password; } public void setPassword(String pass) { this.password = pass; } } and in my message.properties file i write:- NotEmpty={0} must not empty. It shows me error message as: userId must not empty. and password must not empty. But i want to show error message as User Id must not empty. or 'Password must not empty.' I know i can

JSR-303 and JPA overlapping

荒凉一梦 提交于 2019-12-03 17:06:21
Aloha hey! I already use JPA annotations, now I'd like to use JSR-303 annotations to validate attributes as well. My questions is, how well do they interact with each other? If I use @Basic(optional=false) or @NotNull , will I still have to use nullable=false etc.? I haven't decided which validator implementation to use, e.g. hibernate-validator or oVal (You may also suggest others. I know that oVal is not a JSR-303 implementation, but it maps most annotations ). oVal manual (see above) states that certain JPA annotations will be translated into oVal annotations as well. Hibernate-validator

Parametrizable JSR-303 validation values

本秂侑毒 提交于 2019-12-03 16:39:30
I use JSR-303 bean validation and Spring 3 and I need to provide different values for the annotation depending on the use-case. For example, the value of min parameter in @Size(min=?) must be 1 for some validation and 5 for another case and I want to read this values from a properties file. I know the message parameter can be read from ValidationMessages.properties file if provided as a key but what about the other parameter? As outlined by dpb you can use validation groups to specify the same constraint with different attribute values. If you're working with Hibernate Validator as BV

Can you change an annotation message at run time?

纵饮孤独 提交于 2019-12-03 16:30:20
问题 I'm trying to include a dynamic message in my annotation that changes the main body of the text based on the values that are found in the other variables that are passed to it. I set a default message, but when a certain indicator is set, I want to display a different message. Is this possible? Here's my annotation - @Target({TYPE, ANNOTATION_TYPE}) @Retention(RUNTIME) @Constraint(validatedBy = FieldMatchValidator.class) @Documented public @interface FieldMatch { Class<?>[] groups() default {

@Pattern for alphanumeric string - Bean validation

两盒软妹~` 提交于 2019-12-03 16:30:01
问题 I have a variable name in a bean. I want to add @Pattern validation to accept only alphanumeric. Currently, I have this one. @NotNull @Pattern(regexp = "{A-Za-z0-9}*") String name; But the error is Invalid regular expression. I tried [A-Za-z0-9] . But this is not working either. No errors though. It shows any valid input as failed. 回答1: Do you try this pattern: ^[A-Za-z0-9]*$ or ^[A-Za-z0-9]+$ to avoid empty results. If you want to check that a string contains only specific characters, you