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

原因

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

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

模拟

在这里插入图片描述

proceed异常后,obj将是null。
这里手动处理了null值,避免异常在这里插入图片描述

更优雅的方式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

发布了116 篇原创文章 · 获赞 44 · 访问量 31万+

猜你喜欢

转载自blog.csdn.net/thewindkee/article/details/99437068