原因

接口是返回基本类型(primitive),切面拦截后返回了null。

Null return value from advice does not match primitive return type for: public int …

模拟

SpringAop时Null return value from advice does not match primitive return type for: public int...异常

proceed异常后,obj将是null。
这里手动处理了null值,避免异常SpringAop时Null return value from advice does not match primitive return type for: public int...异常

更优雅的方式1

接口使用包装类而不是基本类型。

更优雅的方式2

避免在aop期间改变返回值。

更优雅的方式3

import com.google.common.base.Defaults;
Defaults.defaultValue(Integer.TYPE);//Defaults.defaultValue(int.class);

更优雅的方式4

Array.get(Array.newInstance(clazz, 1), 0)

参考

spring aop Null return value from advice does not match primitive return type for总结
Getting default value for primitive types - on stackoverflow

相关文章:

  • 2022-01-30
  • 2021-11-13
  • 2021-12-05
  • 2022-12-23
  • 2021-12-02
  • 2022-12-23
  • 2021-05-28
  • 2022-12-23
猜你喜欢
  • 2021-11-06
  • 2022-12-23
  • 2021-12-03
  • 2021-12-01
  • 2021-07-18
  • 2021-06-03
相关资源
相似解决方案