Answer the question
In order to leave comments, you need to log in
Why doesn't Hibernate validate work?
As you know , in the controller, when passing the name of the argument in the request, which must be an Enum, there is a functionality for automatically converting this argument to one of the Enum's values.
Here is what I have registered in the controller FormatterRegistry
:
@Override
public void addFormatters(FormatterRegistry registry) {
registry.addConverter(new StringToColumnNameConverter());
}
StringToColumnNameConverter
inside with Hibernate validation (@NotBlank):public class StringToColumnNameConverter implements Converter<String, ColumnName> {
@Override
public ColumnName convert(@NotBlank(message = PropertyKey.VALIDATION_DATABASE_COLUMN_NAME_NOT_BLANK) String source) {
return ColumnName.valueOf(source.toUpperCase());
}
}
columnName
):@GetMapping(params = ParameterName.SORT_BY)
public Set<GiftCertificateDto> findAllSorted(@RequestParam(value = ParameterName.SORT_BY)
@NotEmpty(message = PropertyKey.VALIDATION_GIFT_CERTIFICATE_COLUMN_MANE_NOT_EMPTY)
Set<ColumnName> columnNames) {
return service.findAllSorted(columnNames);
}
@NotEmpty(message = PropertyKey.VALIDATION_GIFT_CERTIFICATE_COLUMN_MANE_NOT_EMPTY)
works out with a bang. But if I pass a string containing only spaces of the form , then the second annotation @NotBlank
inside StringToColumnNameConverter
for some reason does not work out. Why? @Validated
the class StringToColumnNameConverter
; @Validated
method ;
- added annotation :convert(...)
StringToColumnNameConverter
StringToColumnNameConverter
@Valid
public class StringToColumnNameConverter implements Converter<@Valid String, ColumnName>
@Valid
:@GetMapping(params = ParameterName.SORT_BY)
public Set<GiftCertificateDto> findAllSorted(@RequestParam(value = ParameterName.SORT_BY)
@NotEmpty(message = PropertyKey.VALIDATION_GIFT_CERTIFICATE_COLUMN_MANE_NOT_EMPTY)
Set<@Valid ColumnName> columnNames) {
return service.findAllSorted(columnNames);
}
@NotBlank
- The annotated element must not be null and must contain at least one non-whitespace character. Accepts CharSequence.
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question