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