bean-validation

Making javax validation error message more specific

柔情痞子 提交于 2019-11-30 12:06:38
Sorry if this question has been covered somewhere before. If it has please link me to it, I haven't been able to find a satisfactory answer as yet. I've been looking around trying to find a way to have the error messages provided by my javax validation more specific. The message I currently have for my @Min annotation is specified in the ValidationMessages.properties file: javax.validation.constraints.Min.message=The value of this variable must be less than {value}. and this prints out as would be expected The value of this variable must be less than 1 What I would like to have is the message

Get field name when javax.validation.ConstraintViolationException is thrown

梦想与她 提交于 2019-11-30 11:39:09
When the PathVariable 'name' doesn't pass validation a javax.validation.ConstraintViolationException is thrown. Is there a way to retrieve the parameter name in the thrown javax.validation.ConstraintViolationException? @RestController @Validated public class HelloController { @RequestMapping("/hi/{name}") public String sayHi(@Size(max = 10, min = 3, message = "name should have between 3 and 10 characters") @PathVariable("name") String name) { return "Hi " + name; } Stefan Isele - prefabware.com The following Exception Handler shows how it works : @ExceptionHandler(ConstraintViolationException

JSR 303 Bean Validation - Why on getter and not setter?

北战南征 提交于 2019-11-30 10:52:30
I don't understand why JSR 303 (bean validation) is for the getter methods and not setter? Isn't it more logical to put it under setter method since that is the entry point into a field and validation should be checked prior to that? Annotating getters doesn't mean that validation is performed when a getter is invoked. It is just used to identify the property to which a constraint shall apply. The big advantage of putting constraints on (usually public) getters instead on (typically private) fields is that the constraints are part of the type's public API that way. They will even be added to

How to set check order for JSR303 bean validation

本小妞迷上赌 提交于 2019-11-30 10:20:16
I use the JSR303 Bean Validation to check the form input. @NotBlank @Size(min = 4, max = 30) private String name; @NotBlank @Size(max = 100) @Email private String mail; when then name = '' and email = '',the @NotBlank, @Size at name, @NotBlank, @Size, @Email at mail will be checked. I want set the check order, when the previous order is invalid, the next is not checked, for example. @NotBlank(order = 1) @Size(min = 4, max = 30, order = 2) private String name; (above is not support by JSR303) is there a way to implement it for using JSR303? (i think the custom annotation will done, but i don't

How do I use @Valid with Spring MVC's @RequestBody parameters?

戏子无情 提交于 2019-11-30 08:13:34
I'm having an issue with using @Valid on a parameter to a handler method on my @Controller . My code looks like this: @RequestMapping(value=BIBBLE_BOBBLE_URI_PATTERN + "/widgets", method=RequestMethod.POST) @ResponseBody @ResponseStatus(HttpStatus.CREATED) public Widget saveNewWidget( @PathVariable final String username, @PathVariable final String bibbleBobbleName, @Valid @RequestBody final Widget widget, final BindingResult results, final HttpServletRequest request, final HttpServletResponse response) where Widget is the class for one of my domain objects. I'm using the @RequestBody

Validating double and float values using Hibernate Validator - bean validation

岁酱吖の 提交于 2019-11-30 06:56:09
I'm looking for a way to validate a java.lang.Double field in the Spring command bean for its maximum and minimum values (a value must lie between a given range of values) like, public final class WeightBean { @Max(groups={ValidationGroup.class}, value=Double.MAX_VALUE, message="some key or default message") @Min(groups={ValidationGroup.class}, value=1D, message="some key or default message") private Double txtWeight; //Getter and setter. public interface ValidationGroup{} } But both @Max and @Min cannot take a java.lang.Double value. Note that double and float are not supported due to

Using a custom ResourceBundle with Hibernate Validator

我们两清 提交于 2019-11-30 06:55:05
I'm trying to set up a custom message source for Hibernate Validator 4.1 through Spring 3.0. I've set up the necessary configuration: <!-- JSR-303 --> <bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"> <property name="validationMessageSource" ref="messageSource"/> </bean> The translations are served from my message source, but it seems that the replacement tokens in the messages themselves are looked up in the message source, i.e. for a: my.message=the property {prop} is invalid there are calls to look up 'prop' in the messageSource. Going

Multiple Regex @Pattern's for 1 Field?

主宰稳场 提交于 2019-11-30 06:46:14
I was attempted to apply multiple @Pattern annotations to a single field: @Pattern(regexp = "(?=.*[0-9])", message = "Password must contain one digit.") @Pattern(regexp = "(?=.*[a-z])", message = "Password must contain one lowercase letter.") @Pattern(regexp = "(?=.*[A-Z])", message = "Password must contain one uppercase letter.") @Pattern(regexp = "(?=\S+$)", message = "Password must contain no whitespace.") private String password; However, I cannot do this. I want individual messages per violated regex constraint on the password field. Is this possible? My alternative is to use JSF 2.0 f

Hibernate Validator and Jackson: Using the @JsonProperty value as the ConstraintViolation PropertyPath?

百般思念 提交于 2019-11-30 04:45:34
问题 Say I have a simple POJO like below annotated with Jackson 2.1 and Hibernate Validator 4.3.1 annotations: final public class Person { @JsonProperty("nm") @NotNull final public String name; public Person(String name) { this.name = name; } } And I send JSON like such to a web service: {"name": null} Hibernate when it reports the ConstraintViolation uses the class member identifier "name" instead of the JsonProperty annotation value. Does anyone know if it is possible to make the Hibernate

Implementing custom validation logic for a spring boot endpoint using a combination of JSR-303 and Spring's Validator

随声附和 提交于 2019-11-30 04:02:19
I'm trying to implement some custom validation logic for a spring boot endpoint using a combination of JSR-303 Bean Validation API and Spring's Validator . Based on the Validator class diagram it appears to be possible to extend one of CustomValidatorBean , SpringValidatorAdapter or LocalValidatorFactoryBean to add some custom validation logic into an overridden method validate(Object target, Errors errors) . . However, if I create a validator extending any of these three classes and register it using @InitBinder its validate(Object target, Errors errors) method is never invoked and no