尝试连接到 WSDL 网络服务时未执行代码
Code not executed when trying to connecting to WSDL webservice
尝试使用网络服务时,抛出 NullPointerException,应用程序日志中缺少预期的日志语句。
我在以下代码中得到一个 NullPointerException:
public void useWebservice() {
initEndpoint();
try {
port.usefulFunctionWebserviceProvides(); // NullPointerException is thrown here!
} catch (javax.xml.ws.soap.SOAPFaultException ex) {
log.error("Something went wrong making a request to the webservice");
}
}
initEndpoint 方法如下所示:
private volatile Webservice service = null; // instance variable
private WebservicePort port = null; // instance variable
private void initEndpoint() {
String username = "username"; // Loaded from a properties file
String password = "password"; // Loaded from a properties file
LoginResponse loginResponse;
Webservice theService = service;
if (theService == null || port == null) {
synchronized (this) {
theService = service;
if (theService == null) {
try {
log.info("Initializing Endpoint (service & port)"); // This line appears in the log
service = new Webservice();
port = service.getWebservicePort();
final String wsdlUrl = properties.getProperty(WSDL_URL, WSDL_DEFAULT_URL);
((BindingProvider)port).getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, wsdlUrl);
log.info("EndpointAddress set");
LoginRequestType loginRequest = new LoginRequestType();
loginRequest.setUsername(username);
loginRequest.setPassword(password);
loginResponse = port.login(loginRequest, null, null);
} catch (Exception e) {
try {
log.info("re-Initializing Endpoint (service & port)");
// Try to connect to the webservice using a fallback URL
} catch (Exception e2) {
log.error("Couldn't connect to webservice");
service = null;
throw new CustomException();
}
}
if (loginResponse == null) {
service = null;
throw new CustomException();
}
}
}
}
}
None 的信息记录被打印到日志文件中,除了日志消息 "Initializing Endpoint (service & port)".
我不明白 useWebservice 方法中端口变量怎么可以为空。我还希望在日志文件中看到 "EndpointAddress set" 或 "re-Initializing Endpoint (service & port)"。但两者都没有打印到文件中。
代码之前运行良好,但在移动到新项目并用作依赖项后开始出现问题。
问题已确定,是缺少依赖项。因此,当代码尝试初始化端口时会抛出一个错误。 catch 块只捕获异常,因此代码永远不会被执行。例如,如果我们捕获了 throwable,我们将看到来自 catch 块的日志语句。
PS:在问题的 initEndpoint 方法中完成的初始化端口对象不是线程安全的!需要为每个请求初始化端口。
尝试使用网络服务时,抛出 NullPointerException,应用程序日志中缺少预期的日志语句。
我在以下代码中得到一个 NullPointerException:
public void useWebservice() {
initEndpoint();
try {
port.usefulFunctionWebserviceProvides(); // NullPointerException is thrown here!
} catch (javax.xml.ws.soap.SOAPFaultException ex) {
log.error("Something went wrong making a request to the webservice");
}
}
initEndpoint 方法如下所示:
private volatile Webservice service = null; // instance variable
private WebservicePort port = null; // instance variable
private void initEndpoint() {
String username = "username"; // Loaded from a properties file
String password = "password"; // Loaded from a properties file
LoginResponse loginResponse;
Webservice theService = service;
if (theService == null || port == null) {
synchronized (this) {
theService = service;
if (theService == null) {
try {
log.info("Initializing Endpoint (service & port)"); // This line appears in the log
service = new Webservice();
port = service.getWebservicePort();
final String wsdlUrl = properties.getProperty(WSDL_URL, WSDL_DEFAULT_URL);
((BindingProvider)port).getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, wsdlUrl);
log.info("EndpointAddress set");
LoginRequestType loginRequest = new LoginRequestType();
loginRequest.setUsername(username);
loginRequest.setPassword(password);
loginResponse = port.login(loginRequest, null, null);
} catch (Exception e) {
try {
log.info("re-Initializing Endpoint (service & port)");
// Try to connect to the webservice using a fallback URL
} catch (Exception e2) {
log.error("Couldn't connect to webservice");
service = null;
throw new CustomException();
}
}
if (loginResponse == null) {
service = null;
throw new CustomException();
}
}
}
}
}
None 的信息记录被打印到日志文件中,除了日志消息 "Initializing Endpoint (service & port)".
我不明白 useWebservice 方法中端口变量怎么可以为空。我还希望在日志文件中看到 "EndpointAddress set" 或 "re-Initializing Endpoint (service & port)"。但两者都没有打印到文件中。
代码之前运行良好,但在移动到新项目并用作依赖项后开始出现问题。
问题已确定,是缺少依赖项。因此,当代码尝试初始化端口时会抛出一个错误。 catch 块只捕获异常,因此代码永远不会被执行。例如,如果我们捕获了 throwable,我们将看到来自 catch 块的日志语句。
PS:在问题的 initEndpoint 方法中完成的初始化端口对象不是线程安全的!需要为每个请求初始化端口。