Spring Boot搭建Restful API输出Json数据异常
2017-8-7
| 2023-6-9
0  |  0 分钟
type
status
date
slug
summary
tags
category
icon
password
标签

背景

在使用SpringBoot搭建RESTful API时,在没有实体关联关系时一切正常,但在实体类中加入实体关联时,当MyBatis查询出数据实体类使用jackson序列化为json时,返回页面的时候报如下错误:
2017-08-07 11:18:33.007 WARN 12332 --- [nio-9091-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Failed to write HTTP message: org.springframework.http.converter.HttpMessageNotWritableException: Could not write content: No serializer found for class org.apache.ibatis.executor.loader.javassist.JavassistProxyFactory$EnhancedResultObjectProxyImpl and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: java.util.HashMap["data"]->java.util.ArrayList[0]->com.iws.model.Record_$$_jvst129_0["handler"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: No serializer found for class org.apache.ibatis.executor.loader.javassist.JavassistProxyFactory$EnhancedResultObjectProxyImpl and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: java.util.HashMap["data"]->java.util.ArrayList[0]->com.iws.model.Record_$$_jvst129_0["handler"]) 2017-08-07 11:18:33.002 DEBUG 12332 --- [nio-9091-exec-1] .m.m.a.ExceptionHandlerExceptionResolver : Resolving exception from handler [public java.util.Map com.iws.web.api.RecordApiController.selectByAccountId(java.lang.String,java.lang.String,java.lang.String)]: org.springframework.http.converter.HttpMessageNotWritableException: Could not write content: No serializer found for class org.apache.ibatis.executor.loader.javassist.JavassistProxyFactory$EnhancedResultObjectProxyImpl and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: java.util.HashMap["data"]->java.util.ArrayList[0]->com.iws.model.Record_$$_jvst129_0["handler"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: No serializer found for class org.apache.ibatis.executor.loader.javassist.JavassistProxyFactory$EnhancedResultObjectProxyImpl and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: java.util.HashMap["data"]->java.util.ArrayList[0]->com.iws.model.Record_$$_jvst129_0["handler"]) 2017-08-07 11:18:33.003 DEBUG 12332 --- [nio-9091-exec-1] .w.s.m.a.ResponseStatusExceptionResolver : Resolving exception from handler [public java.util.Map com.iws.web.api.RecordApiController.selectByAccountId(java.lang.String,java.lang.String,java.lang.String)]: org.springframework.http.converter.HttpMessageNotWritableException: Could not write content: No serializer found for class org.apache.ibatis.executor.loader.javassist.JavassistProxyFactory$EnhancedResultObjectProxyImpl and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: java.util.HashMap["data"]->java.util.ArrayList[0]->com.iws.model.Record_$$_jvst129_0["handler"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: No serializer found for class org.apache.ibatis.executor.loader.javassist.JavassistProxyFactory$EnhancedResultObjectProxyImpl and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: java.util.HashMap["data"]->java.util.ArrayList[0]->com.iws.model.Record_$$_jvst129_0["handler"]) 2017-08-07 11:18:33.004 DEBUG 12332 --- [nio-9091-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Resolving exception from handler [public java.util.Map com.iws.web.api.RecordApiController.selectByAccountId(java.lang.String,java.lang.String,java.lang.String)]: org.springframework.http.converter.HttpMessageNotWritableException: Could not write content: No serializer found for class org.apache.ibatis.executor.loader.javassist.JavassistProxyFactory$EnhancedResultObjectProxyImpl and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: java.util.HashMap["data"]->java.util.ArrayList[0]->com.iws.model.Record_$$_jvst129_0["handler"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: No serializer found for class org.apache.ibatis.executor.loader.javassist.JavassistProxyFactory$EnhancedResultObjectProxyImpl and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: java.util.HashMap["data"]->java.util.ArrayList[0]->com.iws.model.Record_$$_jvst129_0["handler"])

解决方法

在关联类中引用 @JsonIgnoreProperties注解,并将类中关联到的类加到注解的value值里面去。
@JsonIgnoreProperties(value={"handler","account"})
其中 handler必须添加!
此处暂时不明白 headler在何处转化,查资料显示 @JsonIgnore注解跟 @JsonIgnoreProperties作用一致,但无法对 handler进行注解。

附相关注解解释

@Transient

@Transient表示该属性并非一个到数据库表的字段的映射,ORM框架将忽略该属性; 如果一个属性并非数据库表的字段映射,就务必将其标示为 @Transient,否则ORM框架默认其注解为 @Basic
//表示该字段在数据库表中没有 @Transient private Account account;

@JsonIgnoreProperties

此注解是类注解,作用是 json序列化时将 java bean中的一些属性忽略掉,序列化和反序列化都受影响。
@JsonIgnoreProperties(value = {"handler", "account"}) public class Record { private Account account; }

@JsonIgnore

此注解用于属性或者方法上(最好是属性上),作用和上面的 @JsonIgnoreProperties一样。
public class Record { @JsonIgnore private Account account; }

@JsonFormat

此注解用于属性或者方法上(最好是属性上),可以方便的把 Date类型直接转化为我们想要的模式,比如
@JsonFormat(pattern = "yyyy-MM-dd HH-mm-ss") private Date date;

@JsonSerialize

此注解用于属性或者 getter方法上,用于在序列化时嵌入我们自定义的代码,比如序列化一个 double时在其后面限制两位小数点。

@JsonDeserialize

此注解用于属性或者 setter方法上,用于在反序列化时可以嵌入我们自定义的代码,类似于上面的 @JsonSerialize
SpringBoot
  • SpringBoot
  • Restful
  • 服务发现比较:Consul vs Zookeeper vs Etcd vs EurekaSpring Boot 采用Thymeleaf模块及Thymeleaf语法
    目录