| import org.springframework.beans.BeansException; |
| import org.springframework.beans.factory.NoSuchBeanDefinitionException; |
| import org.springframework.context.ApplicationContext; |
| import org.springframework.context.ApplicationContextAware; |
| |
| import java.util.Timer; |
| import java.util.TimerTask; |
| |
| public class SpringContextUtil implements ApplicationContextAware { |
| private static ApplicationContext applicationContext; |
| |
| public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { |
| SpringContextUtil.applicationContext = applicationContext; |
| } |
| |
| public static ApplicationContext getApplicationContext() { |
| return applicationContext; |
| } |
| |
| public static Object getBean(String name) throws BeansException { |
| return applicationContext.getBean(name); |
| } |
| |
| public static Object getBean(String name, Class<?> requiredType) throws BeansException { |
| return applicationContext.getBean(name, requiredType); |
| } |
| |
| public static Object getBean(Class<?> requiredType) throws BeansException { |
| return applicationContext.getBean(requiredType); |
| } |
| |
| public static boolean containsBean(String name) { |
| return applicationContext.containsBean(name); |
| } |
| |
| public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException { |
| return applicationContext.isSingleton(name); |
| } |
| |
| public static Class<?> getType(String name) throws NoSuchBeanDefinitionException { |
| return applicationContext.getType(name); |
| } |
| |
| public static String[] getAliases(String name) throws NoSuchBeanDefinitionException { |
| return applicationContext.getAliases(name); |
| } |
| } |
| |
| |
| import com.fasterxml.jackson.core.JsonGenerator; |
| import com.fasterxml.jackson.databind.BeanProperty; |
| import com.fasterxml.jackson.databind.JsonMappingException; |
| import com.fasterxml.jackson.databind.JsonSerializer; |
| import com.fasterxml.jackson.databind.SerializerProvider; |
| import com.fasterxml.jackson.databind.ser.ContextualSerializer; |
| import com.ys.szygl.util.SpringContextUtil; |
| import org.apache.commons.collections.CollectionUtils; |
| import org.apache.commons.collections.MapUtils; |
| import org.apache.commons.lang3.StringUtils; |
| |
| import java.io.IOException; |
| import java.util.*; |
| import java.util.stream.Collectors; |
| |
| public class JsonDicHandle extends JsonSerializer<Object> implements ContextualSerializer { |
| |
| final String DIC_FIELD_SUFFIX = "_dic"; |
| |
| String tableName = ""; |
| |
| String fieldName = ""; |
| |
| DicHandleStrategy strategy = DicHandleStrategy.add; |
| |
| String newFieldName = ""; |
| |
| String propertyName = ""; |
| |
| static final IDicService iDicService = (IDicService)SpringContextUtil.getBean(IDicService.class); |
| |
| public JsonDicHandle() { |
| } |
| |
| public JsonDicHandle(String tableName, String fieldName, DicHandleStrategy strategy, String newFieldName, String propertyName) { |
| this.tableName = tableName; |
| this.fieldName = fieldName; |
| this.strategy = strategy; |
| this.newFieldName = newFieldName; |
| this.propertyName = propertyName; |
| } |
| |
| @Override |
| public void serialize(Object value, JsonGenerator gen, SerializerProvider serializers) throws IOException { |
| if (StringUtils.isBlank(this.newFieldName) && StringUtils.isNotBlank(this.propertyName)) { |
| this.newFieldName = this.propertyName + DIC_FIELD_SUFFIX; |
| } |
| String dicValue = null; |
| if (Objects.nonNull(value)) { |
| dicValue = this.getDicValue(this.tableName, this.fieldName, String.valueOf(value)); |
| if (DicHandleStrategy.replace.name().equals(this.strategy.name())) { |
| gen.writeObject(dicValue); |
| } else { |
| gen.writeObject(value); |
| } |
| } else { |
| gen.writeObject(value); |
| } |
| if (DicHandleStrategy.add.name().equals(this.strategy.name()) && StringUtils.isNotBlank(this.newFieldName)) { |
| gen.writeStringField(this.newFieldName, dicValue); |
| } |
| } |
| |
| @Override |
| public JsonSerializer<?> createContextual(SerializerProvider prov, BeanProperty property) throws JsonMappingException { |
| JsonDicField annotation = property.getAnnotation(JsonDicField.class); |
| if (Objects.nonNull(annotation)) { |
| return new JsonDicHandle(annotation.tableName(), annotation.fieldName(), annotation.strategy(), |
| annotation.newFieldName(), property.getName()); |
| } |
| return new JsonDicHandle(); |
| } |
| |
| |
| * 把值转为字典值 |
| * @param value |
| * @return |
| */ |
| private String getDicValue(String tableName, String fieldName, String value) { |
| String dicValue = null; |
| if (Objects.nonNull(iDicService)) { |
| Map<String, String> dicMap = this.iDicService.getDicMapFromCatch(tableName, fieldName); |
| if (MapUtils.isNotEmpty(dicMap)) { |
| List<String> dicValues = Arrays.stream(value.split(",")).map(s -> { |
| return Optional.ofNullable(dicMap.get(s)).map(String::valueOf).orElse(""); |
| }).collect(Collectors.toList()); |
| if (CollectionUtils.isNotEmpty(dicValues)) { |
| dicValue = String.join(",", dicValues); |
| } |
| } |
| } |
| return dicValue; |
| } |
| |
| } |