Untitled
unknown
java
3 years ago
1.5 kB
9
Indexable
protected <T> T mapPayloadToObject(final byte[] message, final Class<T> clazz) {
try {
T object = JSON_MAPPER.readValue(message, clazz);
final Map<String, Object> map = JSON_MAPPER.readValue(message, new TypeReference<>() {
});
final List<Field> fieldsClass = Arrays.asList(clazz.getDeclaredFields());
List<String> missingAttributes = fieldsClass.stream()
.filter(field -> !map.containsKey(getJsonPropertyName(field)))
.map(this::getJsonPropertyName)
.toList();
List<String> extraAttributes = map.keySet().stream()
.filter(attribute -> fieldsClass.stream()
.noneMatch(field -> attribute.equals(getJsonPropertyName(field))))
.toList();
if (!missingAttributes.isEmpty()) {
log.warnv("Missing attributes for payload: {0} - Missing attributes: {1} for {2}", map, missingAttributes, clazz.getName());
}
if (!extraAttributes.isEmpty()) {
log.warnv("Extra attributes in payload: {0} - Extra attributes: {1} for {2}", map, extraAttributes, clazz.getName());
}
return object;
} catch (final IOException e) {
log.error("Error trying to decode message using json parsing", e);
throw new RuntimeException(e);
}
}
private String getJsonPropertyName(Field field) {
JsonProperty jsonProperty = field.getAnnotation(JsonProperty.class);
return (jsonProperty != null && !jsonProperty.value().isEmpty()) ? jsonProperty.value() : field.getName();
}Editor is loading...