├── .gitignore ├── README.md ├── src ├── main │ ├── resources │ │ ├── logback.xml │ │ └── META-INF │ │ │ └── spring │ │ │ └── application-context.xml │ └── java │ │ └── com │ │ └── hillert │ │ └── spring │ │ └── validation │ │ ├── service │ │ ├── impl │ │ │ └── DefaultBusinessService.java │ │ └── BusinessService.java │ │ └── Main.java └── test │ ├── resources │ └── logback-test.xml │ └── java │ └── com │ └── hillert │ └── spring │ └── validation │ ├── BaseIntegrationTest.java │ ├── ExampleConfigurationTest.java │ └── BusinessServiceTest.java └── pom.xml /.gitignore: -------------------------------------------------------------------------------- 1 | .classpath 2 | .project 3 | .settings/ 4 | .springBeans 5 | target -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | spring-hibernate-validator-sample 2 | ================================= 3 | 4 | Sample project illustrating the usage of Hibernate Validator's support for method validation together with Spring. 5 | 6 | For more information, please visit: http://hillert.blogspot.com/2011/12/method-validation-with-hibernate.html -------------------------------------------------------------------------------- /src/main/resources/logback.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | %d %5p | %t | %-55logger{55} | %m %n 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /src/test/resources/logback-test.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | %d %5p | %t | %-55logger{55} | %m %n 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /src/main/resources/META-INF/spring/application-context.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 9 | 10 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /src/test/java/com/hillert/spring/validation/BaseIntegrationTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2002-2011 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.hillert.spring.validation; 17 | 18 | import org.junit.runner.RunWith; 19 | import org.springframework.test.context.ContextConfiguration; 20 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 21 | 22 | /** 23 | * 24 | * @author Gunnar Hillert 25 | * 26 | */ 27 | @ContextConfiguration(locations={"classpath:META-INF/spring/application-context.xml"}) 28 | abstract class BaseIntegrationTest { 29 | 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/com/hillert/spring/validation/service/impl/DefaultBusinessService.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2002-2011 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.hillert.spring.validation.service.impl; 17 | 18 | import org.springframework.stereotype.Service; 19 | 20 | import com.hillert.spring.validation.service.BusinessService; 21 | 22 | 23 | /** 24 | * Default implementation of the Business Service. 25 | * 26 | * @author Gunnar Hillert 27 | * @since 1.0 28 | * 29 | */ 30 | @Service 31 | public class DefaultBusinessService implements BusinessService { 32 | 33 | /** {@inheritDoc} */ 34 | public String convertToUpperCase(String input) { 35 | 36 | if ("returnnull".equalsIgnoreCase(input)) { 37 | return null; 38 | } 39 | 40 | return input.toUpperCase(); 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /src/main/java/com/hillert/spring/validation/service/BusinessService.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2002-2011 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.hillert.spring.validation.service; 17 | 18 | import javax.validation.constraints.NotNull; 19 | 20 | import org.hibernate.validator.constraints.NotEmpty; 21 | import org.springframework.validation.annotation.Validated; 22 | 23 | /** 24 | * Simple example of a business layer with method validation contraints. 25 | * 26 | * @author Gunnar Hillert 27 | * @since 1.0 28 | * 29 | */ 30 | @Validated 31 | public interface BusinessService { 32 | 33 | /** 34 | * Convert the provided input String to the upper-case representation. 35 | * 36 | * @param input String to be converted to upper-case 37 | * @return Converted String. Never returns null. 38 | */ 39 | @NotNull(message="Null returns are not permitted") 40 | String convertToUpperCase(@NotEmpty(message="Input must not be null or empty.") 41 | String input); 42 | 43 | } 44 | -------------------------------------------------------------------------------- /src/test/java/com/hillert/spring/validation/ExampleConfigurationTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2002-2011 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.hillert.spring.validation; 17 | 18 | import static org.junit.Assert.assertNotNull; 19 | 20 | import javax.validation.Validator; 21 | 22 | import org.junit.Test; 23 | import org.junit.runner.RunWith; 24 | import org.springframework.beans.factory.annotation.Autowired; 25 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 26 | 27 | import com.hillert.spring.validation.service.BusinessService; 28 | 29 | /** 30 | * 31 | * @author Gunnar Hillert 32 | * 33 | */ 34 | @RunWith(SpringJUnit4ClassRunner.class) 35 | public class ExampleConfigurationTest extends BaseIntegrationTest { 36 | 37 | @Autowired 38 | private BusinessService service; 39 | 40 | @Autowired 41 | private Validator validator; 42 | 43 | @Test 44 | public void testSimpleProperties() throws Exception { 45 | assertNotNull(service); 46 | } 47 | 48 | @Test 49 | public void testValidatorInjected() throws Exception { 50 | assertNotNull(validator); 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /src/test/java/com/hillert/spring/validation/BusinessServiceTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2002-2011 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.hillert.spring.validation; 17 | 18 | import static org.junit.Assert.assertEquals; 19 | import static org.junit.Assert.fail; 20 | 21 | import org.hibernate.validator.method.MethodConstraintViolationException; 22 | import org.junit.Test; 23 | import org.junit.runner.RunWith; 24 | import org.springframework.beans.factory.annotation.Autowired; 25 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 26 | 27 | import com.hillert.spring.validation.service.BusinessService; 28 | 29 | /** 30 | * 31 | * @author Gunnar Hillert 32 | * 33 | */ 34 | @RunWith(SpringJUnit4ClassRunner.class) 35 | public class BusinessServiceTest extends BaseIntegrationTest { 36 | 37 | @Autowired 38 | private BusinessService service; 39 | 40 | @Test 41 | public void testConvertToUpperCase() throws Exception { 42 | assertEquals("HELLO WORLD!", service.convertToUpperCase("hello world!")); 43 | } 44 | 45 | @Test 46 | public void testConvertToUpperCaseWithNullReturn() throws Exception { 47 | 48 | try { 49 | service.convertToUpperCase("returnnull"); 50 | } catch (MethodConstraintViolationException e) { 51 | assertEquals("Null returns are not permitted", e.getConstraintViolations().iterator().next().getMessage()); 52 | return; 53 | } 54 | 55 | fail("Was expecting a ConstraintViolationException."); 56 | } 57 | 58 | @Test 59 | public void testConvertToUpperCaseInputEmpty() throws Exception { 60 | 61 | try { 62 | service.convertToUpperCase(""); 63 | } catch (MethodConstraintViolationException e) { 64 | assertEquals("Input must not be null or empty.", e.getConstraintViolations().iterator().next().getMessage()); 65 | return; 66 | } 67 | 68 | fail("Was expecting a ConstraintViolationException."); 69 | } 70 | 71 | @Test 72 | public void testConvertToUpperCaseInputNull() throws Exception { 73 | 74 | try { 75 | service.convertToUpperCase(null); 76 | } catch (MethodConstraintViolationException e) { 77 | assertEquals("Input must not be null or empty.", e.getConstraintViolations().iterator().next().getMessage()); 78 | return; 79 | } 80 | 81 | fail("Was expecting a ConstraintViolationException."); 82 | } 83 | 84 | } 85 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | com.hillert.spring.validation 7 | spring-hibernate-validator-sample 8 | 1.0.0.BUILD-SNAPSHOT 9 | jar 10 | spring-hibernate-validator-sample 11 | http://blog.hillert.com/ 12 | 13 | 17 | 18 | 19 | 20 | UTF-8 21 | 1.6.4 22 | 4.10 23 | 3.1.0.RELEASE 24 | 25 | 26 | 27 | 28 | junit 29 | junit 30 | 4.7 31 | ${junit.version} 32 | 33 | 34 | org.springframework 35 | spring-test 36 | ${spring.framework.version} 37 | test 38 | 39 | 40 | org.springframework 41 | spring-context 42 | ${spring.framework.version} 43 | 44 | 45 | org.hibernate 46 | hibernate-validator 47 | 4.2.0.Final 48 | 49 | 50 | 51 | 52 | 53 | ch.qos.logback 54 | logback-classic 55 | 1.0.0 56 | 57 | 58 | org.slf4j 59 | slf4j-api 60 | ${slf4j.version} 61 | 62 | 63 | org.slf4j 64 | log4j-over-slf4j 65 | ${slf4j.version} 66 | 67 | 68 | org.slf4j 69 | jcl-over-slf4j 70 | ${slf4j.version} 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | org.apache.maven.plugins 79 | maven-compiler-plugin 80 | 81 | 1.6 82 | 1.6 83 | 84 | 85 | 86 | org.codehaus.mojo 87 | exec-maven-plugin 88 | 1.2 89 | 90 | com.hillert.spring.validation.Main 91 | 92 | 93 | 94 | 95 | 96 | -------------------------------------------------------------------------------- /src/main/java/com/hillert/spring/validation/Main.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2002-2011 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.hillert.spring.validation; 17 | 18 | import java.util.Scanner; 19 | import java.util.Set; 20 | 21 | import org.hibernate.validator.method.MethodConstraintViolation; 22 | import org.hibernate.validator.method.MethodConstraintViolationException; 23 | import org.slf4j.Logger; 24 | import org.slf4j.LoggerFactory; 25 | import org.springframework.context.support.AbstractApplicationContext; 26 | import org.springframework.context.support.ClassPathXmlApplicationContext; 27 | 28 | import com.hillert.spring.validation.service.BusinessService; 29 | 30 | /** 31 | * Executes the example to illustrate the usage of method validation with 32 | * Hibernate Validator together with Spring. 33 | * 34 | * @author Gunnar Hillert 35 | * @version 1.0 36 | * 37 | */ 38 | public final class Main { 39 | 40 | private static final Logger LOGGER = LoggerFactory.getLogger(Main.class); 41 | 42 | private Main() { } 43 | 44 | /** 45 | * Load the Spring Integration Application Context 46 | * 47 | * @param args - command line arguments 48 | */ 49 | public static void main(final String... args) { 50 | 51 | LOGGER.info("\n=========================================================" 52 | + "\n " 53 | + "\n Welcome! " 54 | + "\n " 55 | + "\n For more information please visit: " 56 | + "\n http://blog.hillert.com " 57 | + "\n " 58 | + "\n=========================================================" ); 59 | 60 | final AbstractApplicationContext context = 61 | new ClassPathXmlApplicationContext("classpath:META-INF/spring/*-context.xml"); 62 | 63 | context.registerShutdownHook(); 64 | 65 | final Scanner scanner = new Scanner(System.in).useDelimiter("\n"); 66 | 67 | final BusinessService service = context.getBean(BusinessService.class); 68 | 69 | LOGGER.info("\n=========================================================" 70 | + "\n " 71 | + "\n Please press 'q + Enter' to quit the application. " 72 | + "\n " 73 | + "\n=========================================================" ); 74 | 75 | System.out.println("Please enter a string and press : "); 76 | 77 | while(true){ 78 | 79 | System.out.print("$ "); 80 | String input = scanner.nextLine(); 81 | 82 | LOGGER.debug("Input string: '{}'", input); 83 | 84 | if ("q".equalsIgnoreCase(input)) { 85 | break; 86 | } else if ("null".equals(input)) { 87 | input = null; 88 | } 89 | 90 | try { 91 | System.out.println("Converted to upper-case: " + service.convertToUpperCase(input)); 92 | } catch (MethodConstraintViolationException e) { 93 | Set> constraintViolations = e.getConstraintViolations(); 94 | LOGGER.info("Method Validation failed with {} error(s).", constraintViolations.size()); 95 | 96 | for (MethodConstraintViolation violation : e.getConstraintViolations()) { 97 | LOGGER.info("Method Validation: {}", violation.getConstraintDescriptor()); 98 | } 99 | 100 | } 101 | } 102 | 103 | LOGGER.info("Exiting application...bye."); 104 | 105 | System.exit(0); 106 | 107 | } 108 | } 109 | --------------------------------------------------------------------------------