1. 程式人生 > 資料庫 >微服務中使用Redis遇到問題 Could not resolve type id ** into a subtype

微服務中使用Redis遇到問題 Could not resolve type id ** into a subtype

問題背景:

有多個服務

  • admin-login-service 服務(處理管理員登入)
  • redis-service 服務(提供redis操作)
  • 其他 服務

admin-login-service 服務中驗證登入成功,生成 token 作為 key,登入的 Admin 物件作為 value ,呼叫 redis-service 服務儲存到 redis value存的是物件,不是 json 字串)。然後在呼叫 其他 服務前,在攔截器中從 redis 中取出登入的 Admin 物件判斷是否登入,然後報錯:

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is feign.FeignException: status 500 reading RedisService#get(String); content:
{"timestamp":"2020-02-03T12:02:21.835+0000","status":500,"error":"Internal Server Error","message":"Could not read JSON: Could not resolve type id 'com.cup.ycode.commons.domain.Admin' as a subtype of [simple type,class java.lang.Object]: no such class found\n at [Source: (byte[])\"[\"com.cup.ycode.commons.domain.Admin\",{\"id\":1,\"update_time\":[\"java.util.Date\",\"2020-01-24 19:11:01\"],\"create_time\":[\"java.util.Date\",\"2020-01-24 19:11:07\"],\"account\":\"yangxinhu\",\"phone\":null,\"email\":null}]\"; line: 1,column: 39]; nested exception is com.fasterxml.jackson.databind.exc.InvalidTypeIdException: Could not resolve type id 'com.cup.ycode.commons.domain.Admin' as a subtype of [simple type,column: 39]","path":"/get"}
	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:982) ~[spring-webmvc-5.0.10.RELEASE.jar:5.0.10.RELEASE]
	at org.springframework.web.servlet.FrameworkServlet.doPut(FrameworkServlet.java:888) ~[spring-webmvc-5.0.10.RELEASE.jar:5.0.10.RELEASE]

報錯原因:其他服務 呼叫 redis-service 服務,而 redis-service 服務本身沒有 Admin 這個類存在,導致程式找不到要轉換成的型別。

解決方法:需要將 Admin 所在的依賴加入到 redis-service 服務 與 其他 服務中,這樣從 redis 中取出資料,才知道轉換成什麼型別。