springboot使用jackson序列化报错

背景

最近在使用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的注解理解不够导致的啊,少年,继续努力学习吧!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值