Hej,
Przygotowuję restowe api, które przyjmuje XML'e. Niektóre z pól są wymagane, np
Car car```
W jaki sposób mógłbym zwalidować na wejściu, że pole nie jest wypełnione? Coś na zasadzie @Valid w javax.validation.Valid
Aktualnie korzystam z bardzo brzydkiego rozwiązania (moim zdaniem)
<code>
public static <T> void validateRequired(T target, Class<T> targetClass){
StringBuilder errors = new StringBuilder();
Field[] fields = targetClass.getDeclaredFields();
for (Field field : fields) {
XmlElement annotation = field.getAnnotation(XmlElement.class);
if (annotation != null && annotation.required()) {
try {
field.setAccessible(true);
if (field.get(target) == null) {
if (errors.length() != 0) {
errors.append(" ");
}
String message = String.format("%s: required field '%s' is null.",
targetClass.getSimpleName(),
field.getName());
errors.append(message);
}
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
if(errors.length() != 0){
throw new RuntimeException();
}
}
</code>