java无法在服务中自动连接假客户端
我有一个简单的API,它使用一个假客户端来调用其他API并获取访问令牌。但是今天我不明白为什么我的客户机是空的
你能解释一下为什么我的假客户机在运行时是空的吗
@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
# 1 楼答案
您使用了错误的变量来调用方法,而不是
r2aClient
。您应该使用ise myClient根据新的更改编辑
我猜
@EnableFeignClient
找不到类MyClient,试试@EnableFeignClient(basePackages = "<your package for MyClient>)
# 2 楼答案
我认为你必须把
Service
注入你的Controller
。 您将服务创建为普通Java类,因此MyClient
不会被注入