【发布时间】:2020-12-09 08:44:04
【问题描述】:
我想使用第三方客户端 API。我想创建一个 ServiceClient 实例并使用 postMessage API。我创建了两个类,ServiceClient 和 ServiceClientAPI 我应该如何绑定呢?非常感谢!
public class ServiceClient {
@Provides
@Singleton
public ServiceClient provideServiceClient() {
return new ServiceClientBuilder()
.withEndpoint()
.withClient(ClientBuilder.newClient())
.withSystem(SystemBuilder.standard().build())
.build();
}
public class ServiceClientAPI {
private static final Logger LOGGER = LoggerFactory.getLogger(ServiceClientAPI.class);
@Inject
private ServiceClient ServiceClient;
public Value postMessage(@NonNull Value value) {
LOGGER.info("Value is " + value);
try {
Response response = ServiceClient.postMessage(value);
return response;
} catch (Exception ex) {
String errMsg = String.format("Error hitting the Service");
LOGGER.error(errMsg, ex);
throw new Exception(errMsg, ex);
}
}
It doesn't work, how should I bind them?
public class ServiceModule extends AbstractModule {
@Override
protected void configure() {
bind(ServiceClient.class).to(ServiceClientAPI.class);
}
}
【问题讨论】:
标签: java dependency-injection guice