spring-validator

Manually call Spring Annotation Validation Outside of Spring MVC

霸气de小男生 提交于 2021-02-11 06:58:31
问题 I have the following test that fails: @Test public void testValidation() { Validator validator = new LocalValidatorFactoryBean(); Map<String, String> map = new HashMap<String, String>(); MapBindingResult errors = new MapBindingResult(map, Foo.class.getName()); Foo foo = new Foo(); foo.setBar("ba"); validator.validate(foo, errors); assertTrue(errors.hasFieldErrors()); } Foo is as follows: import javax.validation.constraints.Size; public class Foo { @Size(min=9, max=9) private String bar; // ..

Spring validation returns long error messages, not just the customized message

我的未来我决定 提交于 2020-07-18 07:52:06
问题 Spring validation returns long error message instead of the customized once. This is the section of code in the dto. public class RequestDto implements Serializable { @NotNull(message="{id.required}") private Long id; } In controller added the @Valid for input. @RequestMapping(value = ApiPath.PATH, method = RequestMethod.POST, produces = { "application/xml", "application/json" }) public @ResponseBody ResultDecorator saveRequest( @Valid @RequestBody RequestDto msaDisabScreenRequestDto) throws

How to validate the size of @RequestParam of type List

浪尽此生 提交于 2020-01-30 05:18:18
问题 I'm creating a Spring-Boot microservice REST API that expects @RequestParam of type List<String> . How can I validate that the list contains a minimum and a maximum amount of values? So far I've tried using @Size(min=1, max=2) which is supposed to support collections as well ( javax.validation.constraints.Size ). I also tried adding @Valid and a BindingResult parameter along with the @Size annotation without success. I'd prefer using a solution similar to the first example using @Size(min=1,

How to pass parameters through Java Validator messages?

最后都变了- 提交于 2020-01-14 14:37:11
问题 Currently, I have something like @NotNull(message="{err.required}") @Size(min=1, message="{err.required}") private String firstName; In my messages.properties , I have err.required={0} is required. It works but it prints out firstName is required. which is ugly. It passed the variable name firstName as parameter in {0} . How do I pass in something like " First name " instead? So it will become First name is required. 回答1: You could do something like this: @NotNull(message="First name {err

How to combine JSR-303 and Spring Validator class in a service layer?

泪湿孤枕 提交于 2019-12-29 05:32:07
问题 I have some model class public class Account { @Email private String email; @NotNull private String rule; } and spring-validator public class AccountValidator implements Validator { @Override public boolean supports(Class aClass) { return Account.class.equals(aClass); } @Override public void validate(Object obj, Errors errors) { Account account = (Account) obj; ValidationUtils.rejectIfEmpty(errors, "email", "email.required"); ValidationUtils.rejectIfEmpty(errors, "rule", "rule.required");

@PreAuthorize Controller invalidates @Inject in @InitBinder

我与影子孤独终老i 提交于 2019-12-24 17:21:57
问题 In a simple controller : @PreAuthorize("hasAuthority('USER')") @Controller @RequestMapping("/test") public class TestController { private Logger logger = LoggerFactory.getLogger(getClass()); @Inject private MyValidator myValidator; @InitBinder("myObj") private void initBinder(WebDataBinder binder) { logger.info("myValidator = {}", myValidator); binder.initDirectFieldAccess(); binder.setValidator(myValidator); } @RequestMapping(value = "/doPost", method = RequestMethod.POST) public String

@Valid annotation is ignored when applied to MultipartFile object

半城伤御伤魂 提交于 2019-12-23 12:28:40
问题 This is my controller. It accepts a multipart/form-data request with two fields, form and file . The form field is a MyObject , the file field is a MultipartFile . Both variables are annotated with @Valid , and accordingly, I would expect Spring to invoke the Validator class of each respective field. However, this only happens with MyObject , and not with MultipartFile . @RequestMapping("/api") @RestController public class Controller { private MyObjectRepository repo; private