spring-validator

How to validate request parameter if it is not a bean in spring MVC?

两盒软妹~` 提交于 2019-12-23 11:15:11
问题 Below is a POST end point in my spring MVC REST service. I want to use spring validation frame work to make sure that list I receive is not empty. How do I do it? Do I have to provide wrapper bean to around listOfLongs ? @RequestMapping(value = "/some/path", method = RequestMethod.POST) @ResponseBody public Foo bar(@Valid @NotEmpty @RequestBody List<Long> listOfLongs) { /* if (listOfLongs.size() == 0) { throw new InvalidRequestException(); } */ // do some useful work } What should be the

How to validate request parameter if it is not a bean in spring MVC?

时光总嘲笑我的痴心妄想 提交于 2019-12-23 11:15:10
问题 Below is a POST end point in my spring MVC REST service. I want to use spring validation frame work to make sure that list I receive is not empty. How do I do it? Do I have to provide wrapper bean to around listOfLongs ? @RequestMapping(value = "/some/path", method = RequestMethod.POST) @ResponseBody public Foo bar(@Valid @NotEmpty @RequestBody List<Long> listOfLongs) { /* if (listOfLongs.size() == 0) { throw new InvalidRequestException(); } */ // do some useful work } What should be the

Inject Repository inside ConstraintValidator with Spring 4 and message interpolation configuration

我的梦境 提交于 2019-12-23 07:38:11
问题 I created a small example project to show two problems I'm experiencing in the configuration of Spring Boot validation and its integration with Hibernate. I already tried other replies I found about the topic but unfortunately they didn't work for me or that asked to disable Hibernate validation. I want use a custom Validator implementing ConstraintValidator<ValidUser, User> and inject in it my UserRepository . At the same time I want to keep the default behaviour of Hibernate that checks for

How can I programmatically call the same validator that runs on a @RequestMethod method with a @Valid parameter in Spring?

烂漫一生 提交于 2019-12-23 07:00:37
问题 I have a class with hibernate's validation annotation on some fields (such as @NotNull and @Size(min = 4, max = 50) , etc...) public class MyClass { Long id; @NotEmpty @Size(min = 4, max = 50) String machineName; @NotEmpty @Size(min = 4, max = 50) String humanName; // Getters, setters, etc… } I also have a custom controller that acts as a JSON API, and a JSON deserializer that creates MyClass objects when API methods are called. In my custom controller I have a method to create a new object

What does this nested annotation do / allow?

折月煮酒 提交于 2019-12-21 09:13:55
问题 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}

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

ぃ、小莉子 提交于 2019-12-21 05:03:25
问题 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?... 回答1: I have finally came across with a

nested exception is java.lang.NoClassDefFoundError: Could not initialize class org.hibernate.validator.internal.engine.ConfigurationImpl

江枫思渺然 提交于 2019-12-19 11:27:52
问题 public ModelAndView Details(@ModelAttribute("") @Validated App app, BindingResult result, @RequestParam(value="paramSessionAttr", required=false) String sessionAttr, @RequestParam("paramAction") @NotNull @NotEmpty String param_action){ ... } When I implement @NotNull @NotEmpty annotation validator in getParam its cause error said : org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'appController' defined in file [C:\apache-tomcat-8.0.29\wtpwebapps

How to include multiple model element in spring form?

余生长醉 提交于 2019-12-13 06:16:16
问题 Hi I am creating two separate bean for one UserVO for storing userId and userPassword.And Second bean CompanyVO for storing rest of the details. I am using spring form.I want to include both UserVO and and CompanyVO in spring form. Here is my code CompanyVO public class CompanyVO { private long companyId; private String comapanyName; private String companySize; private String compnayDescription; private String foundedYear; private String websiteURL; } UserVO public class UserVO { private long

Redirect in post mode inside a controller's method

别等时光非礼了梦想. 提交于 2019-12-12 04:20:07
问题 EDIT: If someone have problems following the guide below, I suggest to use an easier approach, like this one: https://www.youtube.com/watch?v=yaxUV3Ib4vM I'm still following this tutorial: spring-mvc-radiobutton-and-radiobuttons-example and I've created this controller so far: @RequestMapping(value = "add", method = RequestMethod.GET) public String add(Model model) { MyObject object = new MyObject(); object.setParameter("fake parameter"); model.addAttribute("add", object); initModelList(model

How to wrap the entity data and pass to the spring validator

廉价感情. 提交于 2019-12-11 17:23:21
问题 I am trying to wrap the entity data with some other information such as requestor info. Right now, I have the following code, public class EntityController { @Autowired private EntityValidator entityValidator; ... @InitBinder("entity") protected void initBinder(WebDataBinder binder) { binder.addValidators(entityValidator); } } and my validitor is like public class EntityValidator implements Validator { @Override public boolean supports(Class<?> clz) { return Entity.class.equals(clz); }