3 回答
TA贡献1853条经验 获得超18个赞
JsonJavaObject 是您的 POJO java 类吗?如果是,那么请使用 Fasterxml 的 ObjectMapper 将您的 JSON 数据映射到 JsonJavaObject 类的字段。使用此方法将您的 json 数据映射到任何 POJO 类
public final T validateJson(final Map<String, Object> jsonMap, final T temmplateClass) throws Exception {
ObjectMapper objectMapper = new ObjectMapper();
try {
// convert JSON string to Map
String jsonString = objectMapper.writeValueAsString(jsonMap);
return objectMapper.readValue(jsonString, (Class<T>) temmplateClass);
} catch (JsonMappingException e) {
throw new NestableRuntimeException(String.format(ApiCommonConstants.INVALID_JSON_FIELD,
e.getPath().get(e.getPath().size() - 1).getFieldName()));
} catch (IOException e) {
throw new NestableRuntimeException(e.getMessage(), e);
}
}
添加回答
举报