work.shashank.util.EntityUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of entity-events Show documentation
Show all versions of entity-events Show documentation
Events related to Hibernate Entities and Auditing of entities
The newest version!
package work.shashank.util;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.Assert;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
/**
* Utility Class
* @author Shashank Sharma
*/
public class EntityUtils {
private static final Logger log = LoggerFactory.getLogger(EntityUtils.class);
private EntityUtils() {
// No Instance for Utils
}
// Thread Safe class thus static
private static final ObjectMapper mapper = new ObjectMapper();
static {
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
mapper.addMixIn(Object.class, IgnoreHibernatePropertiesInJackson.class);
}
/**
* @param object Object provided
* @param annotationClass {@link java.lang.Class} class Object
* @return String value of property annotated by provided class
*/
public static String getAnnotatedFieldValue(Object object, Class extends Annotation> annotationClass) {
try {
Map fieldMap = new LinkedHashMap<>();
for (Field field: object.getClass().getDeclaredFields()) {
if (field.isAnnotationPresent(annotationClass)) {
field.setAccessible(true);
fieldMap.put(field.getName(), field.get(object));
}
}
switch (fieldMap.size()) {
case 0: return null;
case 1: return fieldMap.entrySet().iterator().next().getValue().toString();
default: return toJson(fieldMap);
}
}catch (Exception e) {
// Ignored
}
return null;
}
/**
* @param object Object provided
* @see com.fasterxml.jackson.databind.ObjectMapper#writeValueAsString(Object)
* @return Json String of specified object
*/
public static String toJson(Object object) {
try {
return mapper.writeValueAsString(object);
} catch (Exception e) {
log.error("Exception occured while converting Object toJson", e);
}
return null;
}
/**
* @param object Object provided
* @see com.fasterxml.jackson.databind.ObjectMapper#convertValue(Object, Class)
* @return {@link java.util.Map} representation of specified object
*/
public static Map convertToMap(Object object) {
Assert.notNull(object, "object cannot be null");
return mapper.convertValue(object, new TypeReference
© 2015 - 2025 Weber Informatics LLC | Privacy Policy