По умолчанию REST Docs ConstraintDescriptions
использует ResourceBundleConstraintDescriptionResolver
, чтобы получить описание для каждого ограничения. Как следует из названия, он использует ResourceBundle
для предоставления описаний. Вы можете предоставить собственную реализацию ConstraintDescriptionResolver
для использования другого механизма. В вашем случае вы хотите использовать message
из аннотации ограничения, как показано в следующем примере:
ConstraintDescriptions descriptions = new ConstraintDescriptions(CategoryDTO.class, (constraint) -> {
return (String) constraint.getConfiguration().get("message");
});
List<String> descriptionProperty = descriptions.descriptionsForProperty("description");
System.out.println(descriptionProperty);
При выполнении вышеприведенного вы получите следующее:
[Description can't be null, Size must be greater than 2 and less than 30]
Если вы не всегда настраиваете атрибут message
, вы можете обратиться к средству разрешения пакетов ресурсов, как показано в следующем примере:
ResourceBundleConstraintDescriptionResolver fallback = new ResourceBundleConstraintDescriptionResolver();
ConstraintDescriptions descriptions = new ConstraintDescriptions(CategoryDTO.class, (constraint) -> {
String message = (String) constraint.getConfiguration().get("message");
if (message != null) {
return message;
}
return fallback.resolveDescription(constraint);
});