J
J
Jake Taylor2021-11-11 19:21:41
Hibernate
Jake Taylor, 2021-11-11 19:21:41

Why doesn't the Exception Handler handle a validation error in a GET request?

The controller has two methods with argument validation, one is GET, the other is POST. Why, in the case of the POST method, the Exception Handler catches the exception when passing invalid data in the form of JSON and displays the response as JSON in Tomcat, while in the case of the GET method, the Exception Handler does not handle the exception and does not return the response in the form of JSON, but only displays the operation status as 400 Bad request?

@PostMapping
public EntityModel<TagDto> create(@Valid @RequestBody TagDto tagDto) {
    // тут срабатывает исключение при вводе невалидных данных и обрабатывается
}

@GetMapping
public CollectionModel<TagDto> findAll(@Valid EsmPagination pagination) {
    //  При вызове данного метода Exception Handler не обрабатывает исключение при невалидных данных
}


Although objects TagDtohave EsmPaginationan equivalent structure inside:
TagDto
@AllArgsConstructor
@RequiredArgsConstructor
@Getter
@Setter
@EqualsAndHashCode(callSuper = false)
public class TagDto extends RepresentationModel<TagDto> {
    private Long id;

    @NotBlank(message = MessagePropertyKey.VALIDATION_TAG_NAME_NOT_EMPTY)
    @Pattern(regexp = DtoRegexValidator.TAG_NAME,
            flags = Pattern.Flag.UNICODE_CASE,
            message = MessagePropertyKey.VALIDATION_TAG_NAME)
    private String name;
}


EsmPagination :
@AllArgsConstructor
@Getter
@Setter
@ToString
public class EsmPagination implements Serializable {
    private static final int MIN_PAGE_INDEX = 1;
    private static final int DEFAULT_ELEMENTS_ON_PAGE = 10;

    @Min(value = MIN_PAGE_INDEX, message = MessagePropertyKey.EXCEPTION_ESM_PAGINATION_PAGE_INCORRECT_VALUE)
    private int page;

    @Positive(message = MessagePropertyKey.EXCEPTION_ESM_PAGINATION_SIZE_INCORRECT_VALUE)
    private int size;

    public EsmPagination() {
        this.page = MIN_PAGE_INDEX;
        this.size = DEFAULT_ELEMENTS_ON_PAGE;
    }
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question