SpringBoot笔记--Jackson
SpringUtil.getBean<GenericConversionService>().addConverter(Date2LocalDateTimeConverter())
var handerAdapter = SpringUtil.context.getBean(RequestMappingHandlerAdapter::class.java);
var listResolvers = mutableListOf<HandlerMethodArgumentResolver>()
listResolvers.add(RequestParameterConverter(listOf(ApiParam::class.java)));
listResolvers.addAll(handerAdapter.argumentResolvers)
handerAdapter.argumentResolvers = listResolvers;
internal class MyJsonMapper
@JvmOverloads private constructor()
: ObjectMapper() {
init {
// 设置输出时包含属性的风格
this.findAndRegisterModules();
this.setSerializationInclusion(JsonInclude.Include.NON_EMPTY)
// 允许单引号、允许不带引号的字段名称
this.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true)
this.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true)
// 设置输入时忽略在JSON字符串中存在但Java对象实际没有的属性
this.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
// 空值处理为空串
this.serializerProvider.setNullValueSerializer(object : JsonSerializer<Any>() {
@Throws(IOException::class, JsonProcessingException::class)
override fun serialize(value: Any, jgen: JsonGenerator,
provider: SerializerProvider) {
jgen.writeString("")
}
})
this.registerModule(CustomModule());
// 进行HTML解码。
// this.registerModule(SimpleModule().addSerializer(String::class.java, object : JsonSerializer<String>() {
// @Throws(IOException::class, JsonProcessingException::class)
// override fun serialize(value: String, jgen: JsonGenerator,
// provider: SerializerProvider) {
// jgen.writeString(StringEscapeUtils.unescapeHtml4(value))
// }
// }))
// 设置时区
this.setTimeZone(TimeZone.getDefault())//getTimeZone("GMT+8:00")
}
companion object {
/**
* 创建只输出非Null且非Empty(如List.isEmpty)的属性到Json字符串的Mapper,建议在外部接口中使用.
*/
val instance: MyJsonMapper = MyJsonMapper();
}
}
// 先定义一个json生成器
class ObjectIdJsonSerializer : JsonSerializer<ObjectId>() {
override fun serialize(o: ObjectId?, j: JsonGenerator, s: SerializerProvider) {
if (o == null) {
j.writeNull()
} else {
j.writeString(o.toString())
}
}
}
class LocalDateJsonSerializer : JsonSerializer<LocalDate>() {
override fun serialize(value: LocalDate?, generator: JsonGenerator, serializers: SerializerProvider) {
if (value == null) {
generator.writeNull()
} else {
generator.writeNumber(Date.from(value.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant()).time)
}
}
}
class LocalTimeJsonSerializer : JsonSerializer<LocalTime>() {
override fun serialize(value: LocalTime?, generator: JsonGenerator, serializers: SerializerProvider) {
if (value == null) {
generator.writeNull()
} else {
generator.writeString(value.AsString())
}
}
}
class LocalDateTimeJsonSerializer : JsonSerializer<LocalDateTime>() {
override fun serialize(value: LocalDateTime?, generator: JsonGenerator, serializers: SerializerProvider) {
if (value == null) {
generator.writeNull()
} else {
generator.writeNumber(Date.from(value.atZone(ZoneId.systemDefault()).toInstant()).time)
}
}
}
class LocalDateJsonDeserializer : JsonDeserializer<LocalDate>() {
override fun deserialize(json: JsonParser?, ctxt: DeserializationContext?): LocalDate? {
if (json == null) {
return null;
}
if (json.valueAsString.contains("-")) {
return json.valueAsString.AsLocalDateTime().toLocalDate();
}
return Date(json.longValue).AsLocalDateTime().toLocalDate();
}
}
class LocalTimeJsonDeserializer : JsonDeserializer<LocalTime>() {
override fun deserialize(json: JsonParser?, ctxt: DeserializationContext?): LocalTime? {
if (json == null) {
return null;
}
if (json.valueAsString.contains(".")) {
return LocalTime.parse(json.valueAsString, DateTimeFormatter.ofPattern("HH:mm:ss.sss"))
}
return LocalTime.parse(json.valueAsString, DateTimeFormatter.ofPattern("HH:mm:ss"))
}
}
class LocalDateTimeJsonDeserializer : JsonDeserializer<LocalDateTime>() {
override fun deserialize(json: JsonParser?, ctxt: DeserializationContext?): LocalDateTime? {
if (json == null) {
return null;
}
if (json.valueAsString.contains("-")) {
return json.valueAsString.AsLocalDateTime();
}
return Date(json.longValue).AsLocalDateTime();
}
}
//Jackson 输出的时候,进行自定义序列化格式。
// http://www.jianshu.com/p/a0fb6559f56d
@Component
class CustomModule() : SimpleModule(PackageVersion.VERSION) {
init {
addSerializer(ObjectId::class.java, ObjectIdJsonSerializer());
addSerializer(LocalDate::class.java, LocalDateJsonSerializer());
addSerializer(LocalTime::class.java, LocalTimeJsonSerializer());
addSerializer(LocalDateTime::class.java, LocalDateTimeJsonSerializer());
addDeserializer(LocalDate::class.java, LocalDateJsonDeserializer())
addDeserializer(LocalTime::class.java, LocalTimeJsonDeserializer())
addDeserializer(LocalDateTime::class.java, LocalDateTimeJsonDeserializer())
}
}
fun <T> T.ToJson(): String {
if (this is String) return this;
return MyJsonMapper.instance.writeValueAsString(this) ?: ""
}
fun <T> String.FromJson(collectionClass: Class<T>): T {
if (collectionClass == String::class.java) {
return this as T
}
var jsonString = this.RemoveComment().Remove("\r\n", "\n")
if (jsonString.isEmpty()) {
return collectionClass.newInstance();
}
return MyJsonMapper.instance.readValue(jsonString, collectionClass)!!
}
inline fun <reified T> String.FromJson(): T {
return this.FromJson(T::class.java)
}
class RequestParameterConverter(var canIgnoreAnnotationClazzes: List<Class<*>> = listOf()) : HandlerMethodArgumentResolver {
override fun supportsParameter(parameter: MethodParameter?): Boolean {
if (parameter == null) return false
if (ServletRequest::class.java.isAssignableFrom(parameter.parameterType)) return false
if (ServletResponse::class.java.isAssignableFrom(parameter.parameterType)) return false
if (HttpSession::class.java.isAssignableFrom(parameter.parameterType)) return false
if (parameter.hasParameterAnnotation(PathVariable::class.java)) return false;
if (parameter.hasParameterAnnotation(CookieValue::class.java)) return false;
if (parameter.hasParameterAnnotation(RequestHeader::class.java)) return false;
if (parameter.hasParameterAnnotation(RequestParam::class.java)) return false;
var parameterAnnotations = parameter.parameterAnnotations.toMutableList();
parameterAnnotations.removeAll { Nullable::class.java.isInstance(it) }
parameterAnnotations.removeAll { NotNull::class.java.isInstance(it) }
canIgnoreAnnotationClazzes.forEach { ignoreClazz ->
parameterAnnotations.removeAll { ignoreClazz.isInstance(it) }
}
if (parameterAnnotations.size > 0) return false;
return true
//return parameter?.hasParameterAnnotation(JsonValue::class.java) ?: false;
}
override fun resolveArgument(parameter: MethodParameter?, mavContainer: ModelAndViewContainer?, webRequest: NativeWebRequest?, binderFactory: WebDataBinderFactory?): Any? {
if (parameter == null || mavContainer == null || webRequest == null || binderFactory == null) return null
var key = parameter.parameterName
var request = (webRequest as ServletWebRequest).request as MyHttpRequestWrapper;
var value: Any? = null
if (request.json.containsKey(key)) {
value = request.json.get(key)
} else if (request.requestMap.contains(key)) {
value = request.requestMap.get(request.queryString);
}
if (value == null || (value is String && value.isEmpty())) {
if (parameter.parameterType == String::class.java) {
return "";
} else if (Number::class.java.isAssignableFrom(parameter.parameterType)) {
return MyUtil.getSimpleClassDefaultValue(parameter.parameterType);
} else if (parameter.parameterType.isArray) {
return "[]".FromJson(parameter.parameterType)
} else if (Iterable::class.java.isAssignableFrom(parameter.parameterType)) {
return "[]".FromJson(parameter.parameterType);
}
return null;
// var nullable = parameter.parameterAnnotations.any { Nullable::class.java.isInstance(it) }
//
// if (nullable) {
// return null;
// }
// return MyUtil.getSimpleClassDefaultValue(parameter.parameterType)
}
if (parameter.parameterType.isEnum) {
return value.AsString().ToEnum(parameter.parameterType)
} else if (value.javaClass.isInstance(parameter.parameterType)) {
return value;
} else if (parameter.parameterType == Boolean::class.java) {
return value.AsBoolean()
} else if (parameter.parameterType == Character::class.java) {
return value.toString()[0]
} else if (parameter.parameterType == Byte::class.java) {
return value.AsInt().toByte()
} else if (parameter.parameterType == Short::class.java) {
return value.AsInt().toShort()
} else if (parameter.parameterType == Int::class.java ||
parameter.parameterType == java.lang.Integer::class.java) {
return value.AsInt()
} else if (parameter.parameterType == Long::class.java) {
return value.AsLong()
} else if (parameter.parameterType == Float::class.java) {
return value.AsDouble()
} else if (parameter.parameterType == Double::class.java) {
return value.AsDouble().toLong()
} else if (parameter.parameterType == String::class.java) {
return value.AsString()
} else if (parameter.parameterType == LocalDate::class.java) {
return value.AsLocalDateTime().toLocalDate()
} else if (parameter.parameterType == LocalTime::class.java) {
return value.AsLocalDateTime().toLocalTime()
} else if (parameter.parameterType == LocalDateTime::class.java) {
return value.AsLocalDateTime()
} else if (parameter.parameterType == Date::class.java) {
return Date.from(value.AsLocalDateTime().atZone(ZoneId.systemDefault()).toInstant());
} else {
return value.AsString().FromJson(parameter.parameterType);
}
return value
}
}
@Configuration
open class MongoConfig {
@Bean
@Throws(Exception::class)
open fun mongoTemplate(): MongoTemplate {
var dbFactory = SpringUtil.getBean<MongoDbFactory>();
//remove _class
val converter = MappingMongoConverter(DefaultDbRefResolver(dbFactory), MongoMappingContext())
converter.typeMapper = DefaultMongoTypeMapper(null)
(converter.conversionService as GenericConversionService).addConverter(Date2LocalDateTimeConverter())
return MongoTemplate(dbFactory, converter)
}
}
class Date2LocalDateTimeConverter : GenericConverter {
override fun getConvertibleTypes(): MutableSet<GenericConverter.ConvertiblePair> {
var pairs = hashSetOf<GenericConverter.ConvertiblePair>();
pairs.add(GenericConverter.ConvertiblePair(String::class.java, LocalDate::class.java));
pairs.add(GenericConverter.ConvertiblePair(String::class.java, LocalTime::class.java));
pairs.add(GenericConverter.ConvertiblePair(String::class.java, LocalDateTime::class.java));
pairs.add(GenericConverter.ConvertiblePair(Date::class.java, LocalDate::class.java));
pairs.add(GenericConverter.ConvertiblePair(Date::class.java, LocalTime::class.java));
pairs.add(GenericConverter.ConvertiblePair(Date::class.java, LocalDateTime::class.java));
pairs.add(GenericConverter.ConvertiblePair(LocalDate::class.java, Date::class.java));
pairs.add(GenericConverter.ConvertiblePair(LocalTime::class.java, Date::class.java));
pairs.add(GenericConverter.ConvertiblePair(LocalDateTime::class.java, Date::class.java));
return pairs;
}
override fun convert(value: Any?, sourceType: TypeDescriptor?, targetType: TypeDescriptor?): Any? {
if (value == null || sourceType == null || targetType == null) return null
var valueClass = sourceType.type;
var targetClass = targetType.type;
if (valueClass == String::class.java) {
var strValue = value.AsString();
if (strValue.isEmpty()) {
return null;
}
if (targetClass == LocalDate::class.java) {
return strValue.AsLocalDateTime().toLocalDate();
}
if (targetClass == LocalTime::class.java) {
return strValue.AsLocalDateTime().toLocalTime()
}
if (targetClass == LocalDateTime::class.java) {
return strValue.AsLocalDateTime();
}
}
if (valueClass == Date::class.java) {
if (targetClass == LocalDate::class.java) {
return value.AsLocalDateTime().toLocalDate();
}
if (targetClass == LocalTime::class.java) {
return value.AsLocalDateTime().toLocalTime();
}
if (targetClass == LocalDateTime::class.java) {
return value.AsLocalDateTime()
}
}
if (targetClass == Date::class.java) {
if (valueClass == LocalDate::class.java) {
return Date.from((value as LocalDate).atStartOfDay().atZone(ZoneId.systemDefault()).toInstant());
}
if (valueClass == LocalTime::class.java) {
return Date.from((value as LocalTime).atDate("1970-01-01".AsLocalDateTime().toLocalDate()).atZone(ZoneId.systemDefault()).toInstant());
}
if (valueClass == LocalDateTime::class.java) {
return Date.from((value as LocalDateTime).atZone(ZoneId.systemDefault()).toInstant());
}
}
return null;
}
}
问题
返回实体时,如:
- JsonTypedResult 类型, 当有消息时, 仅返回: JsonResult
- JsonTypedResult 类型, 当 data.id 为 "000000000" 时, 返回 id= ""
SpringBoot笔记--Jackson的更多相关文章
- springboot之jackson的两种配置方式
springboot 针对jackson是自动化配置的,如果需要修改,有两种方式: 方式一:通过application.yml 配置属性说明:## spring.jackson.date-format ...
- SpringBoot与jackson.databind兼容报错问题
SpringBoot与jackson.databind兼容报错问题 ———————————————— 1.SpringBoot版本V2.0.0其依赖的jackson-databind版本为V2.9.4 ...
- 源码分析springboot自定义jackson序列化,默认null值个性化处理返回值
最近项目要实现一种需求,对于后端返回给前端的json格式的一种规范,不允许缺少字段和字段值都为null,所以琢磨了一下如何进行将springboot的Jackson序列化自定义一下,先看看如何实现,再 ...
- spring学习笔记---Jackson的使用和定制
前言: JAVA总是把实体对象(数据库/Nosql等)转换为POJO对象再处理, 虽然有各类框架予以强力支持. 但实体对象和POJO, 由于"饮食习惯", "民族特色 ...
- SpringBoot笔记一
1 开始 1.1 spring介绍 Spring Boot使开发独立的,产品级别的基于Spring的应用变得非常简单,你只需"just run". 我们为Spring平台及第三方库 ...
- SpringBoot笔记十六:ElasticSearch
目录 ElasticSearch官方文档 ElasticSearch安装 ElasticSearch简介 ElasticSearch操作数据,RESTful风格 存储 检查是否存在 删除 查询 更新 ...
- 让SpringBoot的jackson支持JavaBean嵌套的protobuf
问题背景 REST 项目使用protobuf 来加速项目开发,定义了很多model,vo,最终返回的仍然是JSON. 项目中一般使用 一个Response类, public class Respons ...
- SpringBoot系列——Jackson序列化
前言 Spring Boot提供了与三个JSON映射库的集成: Gson Jackson JSON-B Jackson是首选的默认库. 官网介绍: https://docs.spring.io/spr ...
- SpringBoot全局Jackson配置未生效
在做一个小项目,后台服务第一次用SpringBoot构建.接口使用Json格式,在application.properties中配置如下: spring.jackson.default-propert ...
随机推荐
- iframe在iphone中滚动条无效
在iphone(ios11)中(其他苹果设备未尝试),不论是在微信的浏览器中,还是在其他浏览器中,iframe的滚动条怎么设置都没有效果,页面内容都无法进行滚动; 解决方法: 将iframe包裹在di ...
- php中编码转换方法
php里经常用到编码转换,在这记录一个常用的编码转换方法,字符串.数组.对象都可以使用,使用了递归来解决,比较普通 /* * php中编码转换 * @param $param 需要转换的数据 * @p ...
- rows的参数
ds.Tables[0].Rows[0][0].ToString()中的rows后边的那俩参数分别代表什么 第一个0表示行的索引(如果是0就表示第一行,1表示第二行……)第二个0表示列的索引(如果是0 ...
- Numbers
Encoding style, data structure, more content about the list, use the list as a stack, use the list a ...
- C#常见委托のdelegate定义,Func,Action,Predicate总结
委托,顾名思义,就是让其他代理,本质就是为具有共性方法组定义一个方法模板:(交流可以加qq群:435226676) 委托常见的方式有一般委托显示定义,Func<T,TResult> (T, ...
- Java关于ReentrantLock获取锁和释放锁源码跟踪
通过对ReentrantLock获取锁和释放锁源码跟踪主要想进一步深入学习AQS. 备注:AQS中的waitStatus状态码含义:
- JS控制台打印佛祖加持护身符
console.log([ " _ooOoo_", " o8888888o" ...
- 转载 JavaScript的函数声明与函数表达式的区别
1)函数声明(Function Declaration); // 函数声明 function funDeclaration(type){ return type==="Declaration ...
- Signature Request Warnings & eth_sign学习
https://consensys.zendesk.com/hc/en-us/articles/360004427792-Signature-Request-Warnings-eth-sign Met ...
- jvm内存模型中-栈,方法区,程序计数器是线程安全的
文章转自 https://www.cnblogs.com/myna/p/7567889.html 引文 JDK7及之前版本的方法区(Method Area)和Java堆一样,是各个线程共享的内存区域 ...