글에 앞서서
- 본문은 Spring Framework Version 5의 습득을 위한 글이다.
- 이 글을 상업적 목적으로 쓰지 않았다
Authors
Rod Johnson , Juergen Hoeller , Keith Donald , Colin Sampaleanu , Rob Harrop , Thomas Risberg , Alef Arendsen , Darren Davison , Dmitriy Kopylenko , Mark Pollack , Thierry Templier , Erwin Vervaet , Portia Tung , Ben Hale , Adrian Colyer , John Lewis , Costin Leau , Mark Fisher , Sam Brannen , Ramnivas Laddad , Arjen Poutsma , Chris Beams , Tareq Abedrabbo , Andy Clement , Dave Syer , Oliver Gierke , Rossen Stoyanchev , Phillip Webb , Rob Winch , Brian Clozel , Stephane Nicoll , Sebastien DeleuzeCopyright © 2004-2016
Copies of this document may be made for your own use and for distribution to others, provided that you do not charge any fee for such copies and further provided that each copy contains this Copyright Notice, whether distributed in print or electronically.
Validation, Data Binding, and Type Conversion
3.2. Validation using Spring’s Validator interface
3.2. Validation using Spring’s Validator interface
1 | .Data |
1 | public class PersonValidator implements Validator { |
1 | public interface Errors { |
3.3. Resolving codes to error messages
3.3. Resolving codes to error messages
1 | public interface BindingResult extends Errors {} |
1 | FieldError error = bindingResult.getFieldErrors.get(0); |
errors.rejectValue("a", "field.required")
가 실행됐을 때, FieldError
안의 codes 순서
field.required.argumentName.a
field.required.a
field.required.java.lang.Integer
field.required
3.4. Bean manipulation and the BeanWrapper
Bean
: property(getter, setter)
를 가지고 있는 오브젝트
3.4.1. Setting and getting basic and nested properties
1 |
|
1 | BeanWrapper company = new BeanWrapperImpl(new Company()); |
3.4.2 Built-in PropertyEditor implementations
Registering additional custom PropertyEditors
1 |
|
1 | <bean id="novel" class="Book"> |
1 | public class GenreEditor extends PropertyEditorSupport { |
1 | <bean class="org.springframework.beans.factory.config.CustomEditorConfigurer"> |
사용 예) POST https://localhost:8080/books/1 "{"genre" : "fiction"}"
1 |
|
3.4.2 Built-in PropertyEditor implementations
Using PropertyEditorRegistrars
1 | public final class CustomPropertyEditorRegistrar implements PropertyEditorRegistrar { |
1 | <bean class="org.springframework.beans.factory.config.CustomEditorConfigurer"> |
1 | public final class RegisterUserController extends SimpleFormController { |
3.5. Spring Type Conversion
3.5.1. Converter SPI
1 | public interface Converter<S, T> { |
3.5.2. ConverterFactory
1 | public interface ConverterFactory<S, R> { |
1 | final class StringToEnumConverterFactory implements ConverterFactory<String, Enum> { |
3.5.5. Configuring a ConversionService
1 | <bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean"> |
3.5.6. Using a ConversionService programmatically
1 |
|
1 | public interface ConversionService { |
Spring Field Formatting
3.6.1. Formatter SPI
1 | public interface Formatter<T> extends Printer<T>, Parser<T> { |
1 | // 1000 -> "1,000" |
3.6.2. Annotation-driven Formatting
1 | public interface AnnotationFormatterFactory<A extends Annotation> { |
1 | public DateTimeFormat { |
3.7. Configuring a global date & time format
1 |
|
3.8. Spring Validation
3.8.1. Overview of the JSR-303 Bean Validation API
1 | public class PersonForm { |
Spring에서는 기본적으로 JSR-303 Validation의 구현체로
Hibernate Validation
을 쓰고 있음
3.8.2. Configuring a Bean Validation Provider
1 | <bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/> |
Configuring Custom Constraints
제약 애노테이션
과 제약 로직
으로 이루어짐
1 | ({ElementType.METHOD, ElementType.FIELD}) |
기본적으로
LocalValidatorFactoryBean
은 Spring을 사용하여ConstraintValidator
인스턴스를 생성하는SpringConstraintValidatorFactory
를 구성
이를 통해 커스텀ConstraintValidator
는 다른 Spring bean처럼 의존성 주입함
Spring-driven Method Validation
1 | <bean class="org.springframework.validation.beanvalidation.MethodValidationPostProcessor"/> |