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


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM