Answer the question
In order to leave comments, you need to log in
What is the best way to do hibernate validation with error codes?
Hello!
I have a SAAS project, I decided to use hibernate validation (4.3.0.Final) on it.
The problem is that my SAAS essentially provides only an API and clients need to understand what kind of error came in response to their request. Throwing text in response, such as "% field% must be not null" or "javax.validation.constraints.NotNull.message" I think is wrong, because in the first option, it's just impossible to use, in the second it's somehow not cool, since highly implementation dependent. In general, only some constants or enumerations will allow the client to understand exactly what came in response.
I thought that it is possible to implement something like what Google has in one of the systems ( https://developers.google.com/adwords/api/docs/ref...
In this regard, I thought somehow to use enums instead of message.
But something like: @NotNull(message = MaterialType.IMAGE)
Naturally, java does not allow, since the standard NotNull assumes a String in message.
Then I thought about a class with static properties:
public static class Constants {
public static final String IMAGE = "IMAGE";
}
Answer the question
In order to leave comments, you need to log in
NotNull not only assumes a String, but also that it will be a constant, or rather, any annotation that expects a String assumes so.
public enum MessageType {
NOTNULL("not null!!!"), MINVALUE("min value must be ...");
private final String message;
private MessageType(final String message) {
this.message = message;
}
public String getMessage() {
return message;
}
@Override
public String toString() {
return this.message;
}
}
@NotNull(message = MessageType.NOTNULL.getMessage())
private String userName;
MessageType.NOTNULL.name()
the method that returns the name of Enum'a will not pass.public class ValidationConstants {
private ValidationConstants() {} //Приватный конструктор, что бы нельзя было сделать new
public static final String NOT_NULL = "Not nuull...";
public static final String MIN_VALUE = "Min value must be...";
}
@NotNull(message = ValidationConstants.NOT_NULL)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question