I get a json encoded string and then I decode it in pojo, for example:
String json = ...
final ObjectMapper mapper = new ObjectMapper();
MyPojo obj = mapper.readValue(json, MyPojo.class);
I want to be able to check this input, but I'm not sure if this is the "right way" for this.
Let say that MyPojo is defined as follows:
@JsonIgnoreProperties(ignoreUnknown=true)
class MyPojo {
public static enum Type {
One,
Two,
Three;
@JsonValue
public String value() {
return this.name().toLowerCase();
}
}
private String id;
private Type type;
private String name;
public String getId() {
return this.id;
}
public void setId(String id) {
this.id = id;
}
public Type getType() {
return this.type;
}
public void setType(Type type) {
this.type = type;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
}
There are three things I want to check:
- For all members to matter
- That the value of an enumeration is part of an enumeration
- Check some or all values for some criteria (for example: minimum or maximum length, min or person number, regular expression, etc.)
If the verification fails, I want to return a meaningful and readable message.
, , , , .
, , JsonMappingException, :
try {
MyPojo obj = mapper.readValue(json, MyPojo.class);
}
catch (JsonMappingException e) {
return "invalid value for property: " + e.getPath().get(0).getFieldName();
}
, : invalid value: VALUE for property: PROPERTY?
.