我有一個簡單的 API,它使用 FeignClient 呼叫其他 API 并獲取訪問令牌。但是今天我不明白為什么我的客戶是空的。
你能解釋一下為什么我的假客戶端是 null 嗎?
@SpringBootApplication
@EnableFeignClients
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
我的控制器
@RestController
@RequestMapping("/api")
public class AppController {
private final MyService myService = new MyService();
}
我的服務
@Service
public class MyService {
@Autowired
private MyClient myClient;
public AccessToken applicationLogin(final LoginParameters loginParameters) {
return myClient.getAccessToken(loginParameters);
}
}
我的客戶
@FeignClient(name = "myClient", url = "https://myurl.com")
public interface MyClient {
@PostMapping("/auth/login")
AccessToken getAccessToken(@RequestBody LoginParameters loginParameters);
}
錯誤
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause java.lang.NullPointerException: null
uj5u.com熱心網友回復:
您使用錯誤的變數來呼叫該方法,而不是r2aClient您應該 ise myClient
public AccessToken applicationLogin(final LoginParameters loginParameters) {
return myClient.getAccessToken(loginParameters);
}
根據新更改進行編輯
我猜@EnableFeignClient 是找不到類 MyClient,試試@EnableFeignClient(basePackages = "<your package for MyClient>)
uj5u.com熱心網友回復:
我認為你必須Service在你的Controller. 您將服務創建為普通 Java 類,因此MyClient不會注入。
@RestController
@RequestMapping("/api")
public class AppController {
@Autowired
private final MyService myService;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/314124.html
