背景
最近在使用spring boot开发时,遇到jackson序列化的错误,折腾了好一会儿,这里记录一下。
报错信息
com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class org.springframework.context.expression.StandardBeanExpressionResolver and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: com.goldwind.ngsp.ps.dto.Test$$EnhancerBySpringCGLIB$$d694239f["$$beanFactory"]->org.springframework.beans.factory.support.DefaultListableBeanFactory["beanExpressionResolver"])
实体类代码
@Configuration
@Component
@Data
public class Test {
@Value("${test.name:Steven}")
private String name;
}
Controller接口代码
@Autowired
private Test test;
@GetMapping(value = "/test")
public String test() {
return JsonUtil.toJsonString(test);
}
配置文件
test.name=test
解决方案一
将@Configuration注解中的属性proxyBeanMethods 设置为 false
@Configuration(proxyBeanMethods=false)
// @Component
@Data
public class Test {
@Value("${test.name:Steven}")
private String name;
}
这里需要注意一下,要将@Component注解注释掉
解决方案二
将@Configuration注解替换为@Component注解
@Component
@Data
public class Test {
@Value("${test.name:Steven}")
private String name;
}
反思
这里还是对spring boot的注解理解不够导致的啊,少年,继续努力学习吧!