【问题标题】:How to specify wsdlLocation for local file如何为本地文件指定 wsdlLocation
【发布时间】:2019-11-28 16:34:54
【问题描述】:

我是网络服务客户端游戏的新手,并且使用 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 目录,我在哪里可以找到有关将 catalog.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)

【问题讨论】:

  • 我可以看看你是如何实例化服务的吗?
  • @EmanuelRamirez 感谢您的帮助,但您来晚了大约 5 分钟!很高兴终于发现了我的错误,尽管它似乎与我发布的内容完全无关。
  • 哈哈,我很高兴它解决了:)

标签: java web-services wsdl jax-ws


【解决方案1】:

解决了问题(4天后跟踪问题,我可以在这里问问题后不久解决它......)

这确实是设置端点 URL 的有效方法。这就回答了这个问题。

我从这个reference 采用了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,而不是实例化自动生成的请求类的新实例。这就是引发这两个错误的原因,但我确信修复上述问题有助于通过加强我对这些部分如何协同工作的理解来找到解决方案。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-30
    • 2011-08-03
    • 2017-02-18
    • 2022-08-21
    • 2022-12-05
    • 2017-08-09
    • 2014-09-12
    相关资源
    最近更新 更多