Ihar Hancharenka de904f0157 m
2025-01-03 22:24:14 +03:00

167 строки
6.1 KiB
Plaintext
Исходник Ответственный История

Этот файл содержит неоднозначные символы Юникода

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html
https://docs.spring.io/spring-framework/reference/core/validation/beanvalidation.html
https://docs.spring.io/spring-framework/docs/5.3.x/reference/html/core.html#validation
https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config-validation
Spring Validation
https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#validation-beanvalidation
JSR-303/JSR-349
https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#validation-beanvalidation-overview
http://beanvalidation.org/
http://beanvalidation.org/2.0/
spring-boot-starter-validation
https://spring.io/guides/gs/validating-form-input/
https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/validation/MessageCodesResolver.html
https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/validation/DefaultMessageCodesResolver.html
2024
https://stacktips.com/articles/how-to-validate-the-pathvariable-in-spring-boot
https://medium.com/javarevisited/still-use-if-else-perform-validation-in-spring-6-0-springboot-3-0-4e5e0936dec4
https://habr.com/ru/companies/otus/articles/799987/
2022
https://medium.com/emlakjet/request-validation-with-spring-boot-annotations-515d5cae330e
https://habr.com/ru/company/reksoft/blog/675902/
https://github.com/University-and-Education/Validation
2021
https://reflectoring.io/bean-validation-with-spring-boot/
https://github.com/thombergs/code-examples/tree/master/spring-boot/validation
!!!
https://blog.tejanshrana.com/server-side-input-validation-in-java
https://habr.com/ru/post/536612/
2020
https://habr.com/ru/post/505628/
2019
https://reflectoring.io/bean-validation-anti-patterns/
2018
https://reflectoring.io/bean-validation-with-spring-boot/
https://habr.com/ru/post/424819/
Generic Params Validation in REST controllers
https://habr.com/post/425001/
https://github.com/Okapist/IntegerValidationExample
https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#beans-beans
BeanWrapper i-face
BeanWrapperImpl
setPropertyValue
getPropertyValue
BeanWrapper company = new BeanWrapperImpl(new Company());
// setting the company name..
company.setPropertyValue("name", "Some Company Inc.");
// ... can also be done like this:
PropertyValue value = new PropertyValue("name", "Some Company Inc.");
company.setPropertyValue(value);
// ok, let's create the director and tie it to the company:
BeanWrapper jim = new BeanWrapperImpl(new Employee());
jim.setPropertyValue("name", "Jim Stravinsky");
company.setPropertyValue("managingDirector", jim.getWrappedInstance());
// retrieving the salary of the managingDirector through the company
Float salary = (Float) company.getPropertyValue("managingDirector.salary");
PropertyEditors (java.beans.PropertyEditor)
https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#beans-beans-conversion
https://speakerdeck.com/gunnarmorling/keeping-your-data-sane-with-bean-validation-2-dot-3
@NotNull
@Size(max=64)
@Min(0)
Use the LocalValidatorFactoryBean to configure a default Validator as a Spring bean:
<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/>
LocalValidatorFactoryBean implements both javax.validation.ValidatorFactory and javax.validation.Validator,
as well as Springs org.springframework.validation.Validator
import javax.validation.Validator;
@Service
public class MyService {
@Autowired
private Validator validator;
...
}
Custom Constraints
https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#validation-beanvalidation-spring-constraints
@Target({ElementType.METHOD, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy=MyConstraintValidator.class)
public @interface MyConstraint {
}
import javax.validation.ConstraintValidator;
public class MyConstraintValidator implements ConstraintValidator {
@Autowired;
private Foo aDependency;
...
}
Configuring a DataBinder
https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#validation-binder
Foo target = new Foo();
DataBinder binder = new DataBinder(target);
binder.setValidator(new FooValidator());
// bind to the target object
binder.bind(propertyValues);
// validate the target object
binder.validate();
// get BindingResult that includes any validation errors
BindingResult results = binder.getBindingResult();
A DataBinder can also be configured with multiple Validator instances via dataBinder.addValidators and dataBinder.replaceValidators
https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#mvc-config-validation
It is possible to configure the SpEL expression parser using a parser configuration object
(org.springframework.expression.spel.SpelParserConfiguration)
class Demo {
public List<String> list;
}
// Turn on:
// - auto null reference initialization
// - auto collection growing
SpelParserConfiguration config = new SpelParserConfiguration(true,true);
ExpressionParser parser = new SpelExpressionParser(config);
Expression expression = parser.parseExpression("list[3]");
Demo demo = new Demo();
Object o = expression.getValue(demo);
// demo.list will now be a real collection of 4 entries
// Each entry is a new empty String
SpEL compilation
https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#expressions-spel-compilation
Compiler configuration
https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#expressions-compiler-configuration
Expressions in bean definitions
https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#expressions-beandef
Annotation config
https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#expressions-beandef-annotation-based
The @Value annotation can be placed on fields, methods and method/constructor parameters to specify a default value.
Language Reference
https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#expressions-language-ref