Spring Boot JSR-303/349 configuration

前端 未结 3 1195
北海茫月
北海茫月 2021-02-06 06:22

In my Spring Boot 1.5.1 application I\'m trying to configure support of JSR-303 / JSR-349 validation.

I have added a following annotations @NotNull @S

相关标签:
3条回答
  • 2021-02-06 06:51

    The constraint annotations are meant to applied to JavaBeans. See http://beanvalidation.org/1.0/spec/#constraintsdefinitionimplementation-constraintdefinition

    You have the constraint annotation @NotNull, @Size, etc. applied within the DAO. You must create a Java Bean, e.g. "Person", that wraps those attributes (name, description, etc.), then pass "Person" as a parameter to the Controller method. If you need to use a DAO instead of a controller, it will need to be instrumented to perform the validation. You may be on your own in that regard with regard to AOP, etc., unless something has changed since this post: http://forum.spring.io/forum/spring-projects/container/82643-annotation-driven-jsr-303-validation-on-service-and-dao-tier

    Update: Well, looks like it (method level validation JSR-349) is supported now see http://blog.codeleak.pl/2012/03/how-to-method-level-validation-in.html for an example, similar to Arpit's answer. Updated title of question to reflect this latest JSR.

    0 讨论(0)
  • 2021-02-06 07:03

    Move your validation to interface, as follows:

    import javax.validation.Valid;
    import javax.validation.constraints.NotNull;
    import javax.validation.constraints.Size;
    
    public interface DecisionDao {
    
         Decision create(@Valid @NotNull @Size(min = 1) String name,
                String description, String url, String imageUrl);
    }
    

    Annotate your DecisionDaoImpl with @Validated, as follows:

    import org.springframework.stereotype.Service;
    import org.springframework.validation.annotation.Validated;
    
    @Service
    @Validated
    public class DecisionDaoImpl extends BaseDao implements DecisionDao {
    
        @Override
        public Decision create(String name,
                String description, String url, String imageUrl) {
            System.out.println(name);
            return new Decision();
        }
    
    }
    

    Modify your test case to verify for javax.validation.ConstraintViolationException using assertj or ExpectedException, as follows:

    import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
    
    import javax.validation.ConstraintViolationException;
    
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringRunner;
    import org.springframework.validation.beanvalidation.MethodValidationPostProcessor;
    
    @ContextConfiguration(classes = { TenantTest.Config.class })
    @RunWith(SpringRunner.class)
    public class TenantTest {
    
        @Autowired
        private DecisionDao decisionDao;
    
        @Rule
        public ExpectedException expectedException = ExpectedException.none();
    
        @Test
        public void testCreateDecisionUsingAssertj() {
            assertThatExceptionOfType(ConstraintViolationException.class)
                    .isThrownBy(
                            () -> decisionDao.create(null,
                                    "Root decision 1 description", null, null));
        }
    
        @Test
        public void testCreateDecision() {
           expectedException.expect(ConstraintViolationException.class);
           decisionDao.create(null, "Root decision 1 description", null, null);
        }
    
        @Configuration
        public static class Config {
            @Bean
            public MethodValidationPostProcessor methodValidationPostProcessor() {
                return new MethodValidationPostProcessor();
            }
    
            @Bean
            public DecisionDao decisionDao() {
                return new DecisionDaoImpl();
            }
        }
    }
    

    Make sure you have hibernate-validator in your classpath along with @StanislavL answer:

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
    </dependency>
    

    And an optional dependency for org.assertj.core.api.Assertions.assertThatExceptionOfType, as:

    <dependency>
         <groupId>org.assertj</groupId>
         <artifactId>assertj-core</artifactId>
         <version>3.3.0</version>
         <scope>test</scope>
    </dependency>
    

    For sample example, you can refer arpitaggarwal/jsr-303

    0 讨论(0)
  • 2021-02-06 07:06

    You need @Valid annotation

    Marks a property, method parameter or method return type for validation cascading. Constraints defined on the object and its properties are be validated when the property, method parameter or method return type is validated.

    0 讨论(0)
提交回复
热议问题