SpringValidation etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster
SpringValidation etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster

2 Ocak 2020 Perşembe

SpringValidation @Validated Anotasyonu - Spring'e Özel

Giriş
Açıklaması şöyle
@Valid
Origin: @Valid is part of the Java Bean Validation specification (JSR 380) which Spring supports. Hence, it's not Spring-specific and can be used in other Java contexts too.
...
@Validated
Origin: Unlike @Valid, @Validated is Spring-specific. It extends the functionality of @Valid and provides more flexibility.
@Validated Anotasyonu iki farklı amaç için kullanılabilir.

Maven
Şu satırı dahil ederiz
<dependency>
<groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-validator</artifactId> <version>6.1.5.Final</version> </dependency>

1. Nesneye Değil Parametreye Uygulamak
Açıklaması şöyle. Bu kullanımda @Validated sınıfın üstüne eklenir. Eğer sınıfta JSR-380 kullanan ve metod parametresi olan nesneler varsa otomatikman onlara da uygulanır.
Validating request parameters and path variables is different because we are not validating a Java object like before. Path variables and parameters are primitive types so instead of annotating class fields, we add constraint annotations directly to the method parameter in the controller. In this case @Size annotation to the postcode path variable. We have to add Spring’s @Validated annotation to the controller at the class level, so that Spring can evaluate the constraint annotations on method parameters.
Örnek
Şöyle yaparız. Burada hem @PathVariable olarak işaretli parametreye, hem de User parametresine uygulanıyor
@RestController
@RequestMapping("/v1/users")
@RequiredArgsConstructor
@Validated
public class UserController {

  @GetMapping("/{postcode}")
  public ResponseEntity<List<User>> findAllByPostcode(@Size(min = 4, max = 6)
                                                      @PathVariable String postcode) {
   return ...
  }
}
Örnek
Şöyle yaparız
@Service
@Validated
public class MyBean {

  public Archive findByCodeAndAuthor(@Size(min = 8, max = 10) String code, Author author) {
    return ...
  }
}
2. Aynı Nesneye Farklı Validation Uygulamak İçin - Validation Group
Açıklaması şöyle
In addition to triggering validation just like @Valid, @Validated provides the ability for group-based validation. This is particularly helpful when you want to have different validation rules for the same object based on different contexts (like create vs update operations).
Açıklaması şöyle.
@Validated was added to support "validation groups", i.e. group of fields in the validated bean. This can be used in multi step forms where you may validate name, email, etc.
Örnek - Create ve Update İçin Farklı Doğrulama
Elimizde şöyle bir kod olsun. Burada kullanıcı yaratılırken id alanı boş olmalı, ancak güncellenirken boş olamaz.
public interface UpdateUser {}
public interface CreateUser {}

public class User {

  @Id()
  @Null(groups = CreateUser.class)
  @NotNull(groups = UpdateUser.class)
  private String id;

  @NotEmpty
  private String name;

  @NotNull
  @Valid
  private Address address;
}
Şöyle yaparız. Burada @Validated metod üzerinde, @Valid ise parametrede kullanılıyor
@RestController
@RequestMapping("/v1/users")
@RequiredArgsConstructor
@Validated
public class UserController {

  private final UserService userService;

  @PostMapping
  @Validated(CreateUser.class)
  public ResponseEntity<User> createUser(@Valid
                                         @RequestBody User user) {
    return ResponseEntity.ok(userService.createUser(user));
  }

  @PutMapping
  @Validated(UpdateUser.class)
  public ResponseEntity<User> updateUser(@Valid
                                         @RequestBody User user) {
    return ResponseEntity.ok(userService.updateUser(user));
  }
}
Örnek - Adımlar İçin Farklı Doğrulama
Elimizde şöyle bir kod olsun.
public class Account {

  @NotBlank(groups = {ValidationStepOne.class})
  private String username;

  @Email(groups = {ValidationStepOne.class})
  @NotBlank(groups = {ValidationStepOne.class})
  private String email;

  @NotBlank(groups = {ValidationStepTwo.class})
  @StrongPassword(groups = {ValidationStepTwo.class})
  private String password;

  @NotBlank(groups = {ValidationStepTwo.class})
  private String confirmedPassword;

}
Şöyle yaparız. Burada sadece @Validated parametrede kullanılıyor
@RequestMapping(value = "stepOne")
public String stepOne(@Validated(Account.ValidationStepOne.class) Account account) {...}

@RequestMapping(value = "stepTwo")
public String stepTwo(@Validated(Account.ValidationStepTwo.class) Account account) {...}

8 Kasım 2018 Perşembe

SpringValidation Validator Arayüzü

Giriş
Şu satırı dahil ederiz.
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;
Açıklaması şöyle.
Spring has its own mechanism to do validation. It is not declarative but very flexible and extensible. It is mostly used in SpringMVC to preform validation of command object that are bound to request parameters before they are sent to the controllers. In spring the main validation construct is the Validator interface
Validator yazarken ValidationUtils,StringUtils sınıfları kullanılabilir. Ayrıca @InitBinder ile de Validator rest noktalarına bağlanabilir

supports metodu
Açıklaması şöyle.
The supports method indicates whether the validator can validate a specific class. If so, the validate method can be called to validate an instance of that class. 
validate metodu
Açıklaması şöyle.
The implementation of this method can register any validation errors that it encounters with the passed in Errors instance.

Örnek
Şöyle yaparız.
public class PeopleValidator implements Validator {
  @Override
  public boolean supports(Class<?> clazz) {
    return true;
  }

  @Override
  public void validate(Object target, Errors errors) {
    errors.reject("DIE");
  }
}
Örnek
Şöyle yaparız.
public class PersonValidator implements Validator {

  public boolean supports(Class clazz) {
    return Date.class.equals(clazz);
  }

  public void validate(Object obj, Errors e) {
    String dateString = (String) obj;
    DateTimeFormatter fmt = DateTimeFormat.forPattern("dd/MM/yyyy HH:mm:ss");
    DateTime ourDate = null;
    try {
      fmt.parseDateTime(dateString);
    } catch (IllegalArgumentException e) {
      // your message here
    } finally {
      ourDate = fmt.parseDateTime(dateString);
    }
  }
}