一、問題
項目中有一個controller類,有一個searchById方法,其中isLocal是個默認值為false的boolean參數,用來判斷連接本地還是線上數據庫。
1 @Controller 2 @RequestMapping("api/case_info") 3 class DocEditAction { 4 …… 5 6 @ResponseBody 7 @RequestMapping(value = ["search"], method = [RequestMethod.GET]) 8 fun searchById(id: String, isLocal: Boolean = false): AnalyzeJson { 9 ………… 10 } 11 12 }
項目打成war包放到tomcat上運行時報錯,報錯信息如下:
public final XXX.AnalyzeJson XXX.DocEditAction.searchById(java.lang.String,boolean) to {[/api/case_info/search],methods=[GET]}: There is already 'docEditAction' bean method public static XXX.AnalyzeJson XXX.DocEditAction.searchById$default(XXX.DocEditAction,java.lang.String,boolean,int,java.lang.Object) mapped. 15:24:41 [http-nio-8660-exec-1] ERROR org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:502)- Context initialization failed org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'requestMappingHandlerMapping' defined in org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration: Invocation of init method failed; nested exception is java.lang.IllegalStateException: Ambiguous mapping. Cannot map 'docEditAction' method public final XXX.AnalyzeJson XXX.DocEditAction.searchById(java.lang.String,boolean) to {[/api/case_info/search],methods=[GET]}: There is already 'docEditAction' bean method
奇怪的是,這樣的controller里的帶默認參數的方法有好幾個,以前也沒有報過錯。這期間發生的可能有關的變更有:
1.一個老哥寫了個攔截器,處理了一下所有接收到的請求,“requestMappingHandlerMapping”就是新增加的一個bean
2.kotlin版本從1.2.30升級到了1.2.40
二、原因
沒有找到根本的原因,推測是老哥寫的攔截器那邊有問題
三、解決方案
把方法里面的帶默認值的參數寫在了方法里面,之后重新打包丟到tomcat里啟動就不報錯了。
雖然感覺沒有解決根本上的問題。
四、好像沒什么關系的知識點
kotlin的默認參數方法使用@JvmOverloads可以將方法重載成多個java方法
1 @JvmOverloads fun A(a: Int, b: Int = 0) { 2 …… 3 } 4 會變成 5 6 public void A(int a) {} 7 public void A(int a, int b) {}
kotlin文檔:https://kotlinlang.org/docs/reference/java-to-kotlin-interop.html