我是Web服务客户端游戏的新手,并且使用wsdl2java maven依赖项生成了代码。
我正在将这个项目打包为 .war 文件。
我的问题是我不知道如何使自动生成的客户端跨域到达端点,也不知道如何正确设置客户端的 wsdlLocation。
从后者开始:在自动生成的服务类中,我发现了一个static URL WSDL_LOCATION
属性,以及WebServiceClient注释的"参数?
@WebServiceClient(name = "my_service",
wsdlLocation = "file:/c:/tmp/my_service.wsdl",
targetNamespace = "someAutoGenTargetNS/wsdl")
public class my_service_Service extends Service {
public final static URL WSDL_LOCATION;
public final static QName SERVICE = new QName("someAutoGenTargetNS/wsdl", "my_service");
public final static QName my_service = new QName("http://someAtuoGenTargetNS/wsdl", "my_service");
static {
URL url = null;
try {
url = new URL("file:/c:/tmp/my_service.wsdl");
} catch (MalformedURLException e) {
java.util.logging.Logger.getLogger(my_service_Service.class.getName())
.log(java.util.logging.Level.INFO,
"Can not initialize the default wsdl from {0}", "file:/c:/tmp/my_service.wsdl");
}
WSDL_LOCATION = url;
}
目前,WSDL_LOCATION url 始终设置为 null,因为它找不到指定的文件路径(这是预期的)。我将 wsdl 和 xsd 存储在资源文件夹中,但不知道如何指定到达该文件夹的路径。我试过了
new URL("file:/resources/my_service.wsdl")
new URL("file:/src/main/java/resources/my_service.wsdl")
new URL("classpath:my_service.wsdl")
以及其他不少人。执行此操作的正确方法是什么,如果它需要 xml 目录,我在哪里可以找到有关在哪里放置目录.xml文件的文档。
现在前者:我相信我将端点更改为我想要的位置的实现是正确的,而 wsdlLocation 问题让我头疼。这是我更改端点的实现。如果这看起来不正确,只需指出我使用什么的方向,不需要完整的实现。
private static final QName SERVICE_NAME = new QName(""someAutoGenTargetNS/wsdl"", "my_service");
private URL wsdlURL = my_service_Service.WSDL_LOCATION;
my_service_Service ss;
my_service port;
public my_service_client()
{
//Redacted code for trusting all SSL Certs and hostnameVerifiers as it is not needed for the scope of this question
this.ss = new my_service_Service(this.wsdlURL,SERVICE_NAME);
this.port = ss.getMy_Service();
BindingProvider bp = (BindingProvider) this.port;
Map<String,Object> clientRequestContext = bp.getRequestContext();
clientRequestContext.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, "https://New_Endpoint_IP");
//Basic auth header credentials
clientRequestContext.put(BindingProvider.USERNAME_PROPERTY,"username");
clientRequestContext.put(BindingProvider.PASSWORD_PROPERTY,"password");
}
在问这个问题之前,我试图了解的资源:
1 2 3 4 5 6 7+ 8
最后,我开始意识到我宁愿使用 HTTP 而不是 SOAP,但遗留系统需要遗留交互。
收到的错误使我相信这是一个 wsdlURL 问题:
javax.xml.ws.soap.SOAPFaultException: Internal Error (from client)
org.apache.cxf.binding.soap.SoapFault: Internal Error (from client)
找出问题所在(稍后跟踪问题 4 天,在这里提出问题后我可以很快解决它......
这确实是设置终结点 URL 的有效方法。这回答了这个问题。
我采用了此参考中的classLoader.getResource
方法来设置 wsdlLocation
我的新代码变成了:
@WebServiceClient(name = "my_service",
wsdlLocation = "classpath:my_service.wsdl",
targetNamespace = "someAutoGenTargetNS/wsdl")
public class my_service_Service extends Service {
public final static URL WSDL_LOCATION;
public final static QName SERVICE = new QName("someAutoGenTargetNS/wsdl", "my_service");
public final static QName my_service = new QName("http://someAtuoGenTargetNS/wsdl", "my_service");
static {
URL url = null;
url = my_service.class.getClassLoader().getResource("my_service.wsdl");
WSDL_LOCATION = url;
}
最后,我意识到我尝试使用的最基本的 Web 服务调用错误地设置了请求正文。我已将request_body设置为 null,而不是实例化自动生成的请求类的新实例。这就是引发这两个错误的原因,但我相信修复上述内容有助于通过加强我对这些部分如何协同工作的理解来引导我找到解决方案。