일부 검증 주석과 같은 @Size(min=1,max=5)
는 The String must have a length of {min} to {max} characters.
그들의 주석 매개 변수 매개 변수 오류 메시지를 수 있다는 사실을 고려할 때, 나는 장소 홀더와 일반적으로 파라미터 오류 메시지에 대한 해결책을 발견 등 {0}
, {1}
, ... :
는 등의 자체 검증 제약 조건을 정의 유효성 검사기 클래스 내에서
@Constraint(validatedBy=OnlyCharactersAllowedValidator.class)
@Documented
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface OnlyCharactersAllowed {
String message() default "parameterizedMessage";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
, 반사 모든 주석 매개 변수 (예 :를 저장하는 맵을 확장하는 데 사용할 수 있습니다및 max
@Size
용) 및 추가 매개 변수 유효성 검사 메시지가 같은 속성 정보 파일에 정의되어있는 경우이 솔루션도 작동 키 0
, 1
등 :
public class OnlyCharactersAllowedValidator implements ConstraintValidator<OnlyCharactersAllowed,String>
{
private static final String validCharacters = "abcdefghijklmnopqrstuvwxyz";
@Override
public boolean isValid(String text, ConstraintValidatorContext constraintContext)
{
for (int index = 0; index < text.length; index++) {
String aCharacter = text.substring(index, index+1);
if (validCharacters.indexOf(aCharacter) < 0) {
/* Within this message call the magic happens: {0} is mapped to the invalid character. */
addMessageParameter(constraintContext, aCharacter);
/* Create violation message manually and suppress the default message. */ constraintContext.buildConstraintViolationWithTemplate(constraintContext.getDefaultConstraintMessageTemplate()).addConstraintViolation();
constraintContext.disableDefaultConstraintViolation();
/* No further validation: show only one error message for invalid characters. */
break;
}
}
}
private void addMessageParameter(ConstraintValidatorContext constraintContext, Object... parameter)
{
try
{
/* Get map for parameters (reflection necessary). */
Field descriptorField = ConstraintValidatorContextImpl.class.getDeclaredField("constraintDescriptor");
descriptorField.setAccessible(true);
@SuppressWarnings("unchecked")
ConstraintDescriptorImpl<OnlyCharactersAllowed> descriptor = (ConstraintDescriptorImpl<OnlyCharactersAllowed>) descriptorField.get(constraintContext);
Map<String,Object> attributes = descriptor.getAttributes();
/* Copy immutable Map to new Map. */
Map<String,Object> newAttributes = new HashMap<String,Object>(attributes.size() + parameter.length);
for (String key : attributes.keySet())
{
newAttributes.put(key, attributes.get(key));
}
/* Add given parameters to attributes. */
Integer parameterCounter = 0;
for (Object param : parameter)
{
newAttributes.put(parameterCounter.toString(), param);
parameterCounter++;
}
/* Set new Map in Descriptor (reflection necessary). */
Field attributesField = ConstraintDescriptorImpl.class.getDeclaredField("attributes");
attributesField.setAccessible(true);
attributesField.set(descriptor, newAttributes);
}
catch (NoSuchFieldException | IllegalAccessException | SecurityException | ClassCastException e1)
{
/* Do nothing in case of exception. Unparameterized message is shown. */
}
}
}
추가 할 수 있습니다.
parameterizedMessage = Invalid character: Do not use the {0} character.
안녕하세요 Claas, validCharacters가 사용되지 않고 text.length가 확인되지 않았습니다. 나는 왜 캐릭터를 하나 하나 파싱 하는지를 볼 수 없다. – Stephane
안녕하세요. Stephane, 발언에 감사드립니다. 예제에서 일부 회사의 생산적 코드를 익명화하려고 시도하면서 오류가있었습니다. 이제는 더 합리적인 것이어야합니다. –