1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
| 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(); }
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; }
}
|