objectMapper.canSerialize SpringMVC实体转JSON字符串

 

springMVC中将实体转换为json方法中,判断是否可以写入

1
org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter#canWrite

其中第173行

this.objectMapper.canSerialize(clazz, causeRef)

,判断是否可以序列化指定类。

 

判断是否可以序列化指定实体类.

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
/**
 * Method that can be called to check whether mapper thinks
 * it could serialize an instance of given Class.
 * Check is done
 * by checking whether a serializer can be found for the type.
 *<p>
 * NOTE: since this method does NOT throw exceptions, but internal
 * processing may, caller usually has little information as to why
 * serialization would fail. If you want access to internal {@link Exception},
 * call {@link #canSerialize(Class, AtomicReference)} instead.
 *
 * @return True if mapper can find a serializer for instances of
 *  given class (potentially serializable), false otherwise (not
 *  serializable)
 */
public boolean canSerialize(Class<?> type) {
    return _serializerProvider(getSerializationConfig()).hasSerializerFor(type, null);
}
 
/**
 * Method similar to {@link #canSerialize(Class)} but that can return
 * actual {@link Throwable} that was thrown when trying to construct
 * serializer: this may be useful in figuring out what the actual problem is.
 *
 * @since 2.3
 */
public boolean canSerialize(Class<?> type, AtomicReference<Throwable> cause) {
    return _serializerProvider(getSerializationConfig()).hasSerializerFor(type, cause);
}

  

 

posted @   GordonDicaprio  阅读(907)  评论(1编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 字符编码:从基础到乱码解决
历史上的今天:
2018-01-07 http://blog.csdn.net/mrlin6688/article/details/53496479
2018-01-07 JavaMail与Spring整合
点击右上角即可分享
微信分享提示