Но проверка не работает.
Даже если отправлен ненормальный запрос, достоверность не может быть проверена.
Код: Выделить всё
@PostMapping
public Api requestApi(
@RequestBody @Valid Api request) {
RequestResponse response = testService.testRequest(request.getBody());
return Api.OK(response);
}
Код: Выделить всё
@Data
@NoArgsConstructor
@AllArgsConstructor
public class RequestDto {
@NotBlank
@Size(min = 1, max = 100)
private String address;
@NotNull
private LocalDateTime date;
}
Код: Выделить всё
@PostMapping
public Api requestApi(
@RequestBody @Valid Api request, Errors errors) {
customApiValidator.validate(request.getBody(), errors);
RequestResponse response = testService.testRequest(request.getBody());
return Api.OK(response);
}
Код: Выделить всё
@Component
@RequiredArgsConstructor
@Slf4j
public class CustomApiValidator implements Validator {
private final SpringValidatorAdapter validator;
@Override
public boolean supports(Class clazz) {
return Api.class.isAssignableFrom(clazz);
}
@Override
public void validate(Object target, Errors errors) {
if (target instanceof Api) {
Api api = (Api) target;
Object body = api.getBody();
validator.validate(body, errors);
}
}
}
Как я могу проверить достоверность API?
Подробнее здесь: https://stackoverflow.com/questions/786 ... custom-api