//实现Lits<Long> 转Lits<String>
/**
* @author zhaoliwei
**/
public class LongListFormatSerializer extends JsonSerializer<List<Long>> {
@Override
public void serialize(List<Long> value, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
String text = "";
//是否为空
if (!CollectionUtils.isEmpty(value)) {
try {
List<String> strList = new ArrayList<>(value.size());
for (Long aLong : value) {
strList.add(String.valueOf(aLong));
}
//格式化是否为空
if (!CollectionUtils.isEmpty(strList)) {
jsonGenerator.writeObject(strList);
return;
}
} catch (Exception ignored) {
}
}
jsonGenerator.writeString(text);
}
}
第二步:在字段上使用注解
/**
* 类目list
**/
@JsonSerialize(using = LongListFormatSerializer.class)
private List<Long> categoryIds;