com.godmonth.util.jackson.JacksonObjectFactory Maven / Gradle / Ivy
package com.godmonth.util.jackson;
import org.apache.commons.lang3.exception.ContextedRuntimeException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.Resource;
import com.fasterxml.jackson.databind.ObjectMapper;
/**
* @author shenyue
*/
public class JacksonObjectFactory {
private static final Logger logger = LoggerFactory.getLogger(JacksonObjectFactory.class);
private Resource resource;
private ObjectMapper objectMapper;
private Class objectType;
public void setObjectMapper(ObjectMapper objectMapper) {
this.objectMapper = objectMapper;
}
public void setObjectType(Class objectType) {
this.objectType = objectType;
}
public T createObject() {
try {
return objectMapper.readValue(resource.getInputStream(), objectType);
} catch (Exception e) {
throw new ContextedRuntimeException(e);
}
}
public void setResource(Resource resource) {
this.resource = resource;
}
}