Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7745377
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T09:59:59+00:00 2026-06-01T09:59:59+00:00

I want to use Spring 3 for validation. In the reference documentation, section 6.2

  • 0

I want to use Spring 3 for validation.

In the reference documentation, section 6.2 deals with the Validator interface that we may implement for the classes we want to validate. It’s explained how to create the Validator class but not how to validate objects.

Section 6.7 deals with the use of JSR-303 API and the annotations that come with. It seems that it’s a different way to validate objects (in this case, using annotations without creating Validator classes).

Are there really two ways to use Spring validation or I’m missing something?

One simple question, it’s also specified that an implementation of JSR-303 API must be present on the classpath. One proposed implementation is Hibernate-Validator. Does Spring provide an implementation?

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T10:00:00+00:00Added an answer on June 1, 2026 at 10:00 am

    Yes, there are two ways:

    • Validator Interfaces are the old one, mainly used in spring 2
    • JSR 303 is new, It does not exist while spring 2 was build

    I would recommend to use JSR 303. – In my opinion there is only one reason to use the old validators: that is if you need a LOT of cross field validations, because they are relative hard to implement with JSR 303 (but its is possible)

    To use JSR 303 you need to add an validator implementation, for example Hibernate-Validator (it is the default implementation of JSR 303) (Hibernate-Validator is NOT the Hibernate ORM!, it is only related)

    This is a bit like JPA, there is the common neutral specification (javax.jpa/javax.validation) and the different provider implementations (for JPA for example: EclipseLink or Hibernate)


    This is an example how to test a validation (the goal of the test was to test the validation itself, but I changed the Validator to a common one), anyway it should show you how to test a validator:

    public class NotEmptyTest {
    
        public static class Demo {
    
            @NotEmpty
            String string;
    
            public Demo(final String string) {
                this.string = string;
            }
        }
    
        @Test
        public void testNotEmptysWithValid() {
            /** given: a valid object */
            Demo valid = new Demo("hallo Welt");
    
            ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
            Validator validator = factory.getValidator();
    
            /** then there must not be any constraint violations */
            AssertUtil.isEmpty(validator.validate(valid));
        }
    
        @Test
        public void testNotEmptyInValid() {
            /** given: an invalid object */
            Demo valid = new Demo("");
    
            ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
            Validator validator = factory.getValidator();
    
            /** then there must not be one constraint violation */
            Set<ConstraintViolation<Demo>> result = validator.validate(valid);
    
            AssertUtil.hasSize(1, result);
            ConstraintViolation<Demo> firstItem = result.iterator().next();
            Assert.assertEquals("{org.hibernate.validator.constraints.NotEmpty.message}",
                    firstItem.getMessageTemplate());
        }
    }
    

    But in an Spring MVC Controller it is much easier, you only need to add @Valid to your command object parameter and need to add a BindingResult parameter directly after the command object parameter

    @RequestMapping(method = RequestMethod.POST)
    public ModelAndView create(@Valid UserCreateCommand userCreateCommand,
             BindingResult bindingResult) {
    
        if (bindingResult.hasErrors()) {
            //show form again
        } else {
            //create user and 
            //redirect To Show user
        }
    }
    

    And the UserCreateCommand is just a POJO where the fields are annotated with a lot of JSR303 Validation constraints.

    public class UserCreateCommand {
    
        @Length(min = 2)
        @NotBlank
        private String login;
    
        @Email
        @NotNull
        private String emailAddress;
    
        ....
    
        //GETTER and SETTER
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to use both ContextLoaderListener (so that I can pass Spring Beans to
I want to create a web app that will use wicket, hibernate and spring
I want to use ComponentModel DataAnnotations validate that at least one of two properties
I came from the Spring camp , I don't want to use Spring ,
I've recently started upgrading some applications to use Spring Webflow 2, and I want
I want to use @PreAuthorize annotation on service methods with Spring Security. One of
I want to use cglib as my proxy mechanism for spring. problem is, i
I'm migrating a Spring MVC controller to use the newer style annotations, and want
I want to use a string that I have been using in one aspx.cs
String validation .. I want to validate a string contains only the following characters

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.