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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章