【问题标题】:Java WSDL DHL ClassesJava WSDL DHL 类
【发布时间】:2019-03-16 23:13:30
【问题描述】:

我已经从 DHL WSDL 创建了 Java 类 https://cig.dhl.de/cig-wsdls/com/dpdhl/wsdl/geschaeftskundenversand-api/2.2/geschaeftskundenversand-api-2.2.wsdl.

现在我有所有的类,但没有 Authentifaction 类。 我试试这个

 GKVAPIServicePortTypeProxy port2 = new GKVAPIServicePortTypeProxy();
        port2.setEndpoint("https://cig.dhl.de/services/sandbox/soap");

     CreateShipmentOrderRequest sh = new CreateShipmentOrderRequest();
        //Setting up shipment;
        .. and so on

        CreateShipmentOrderResponse chr = port2.createShipmentOrder(sh);

但只有我得到的是,“(401)需要授权” 如何设置我的身份验证?

【问题讨论】:

  • 请展示您如何生成您的课程。
  • 我已经通过 eclipse 导入生成了。我还有一个 Bindingsub 类,我可以在其中输入用户名和密码,我不知道如何将 Bindingsub 类耦合到 GKVAPIServicePortTypeProxy,当我使用 Bindingsub 类连接到服务时,我没有端点地址

标签: java eclipse soap wsdl dhl


【解决方案1】:

您好,我已经解决了将 ssl 证书从 DHL 添加到我的应用程序信任库的 401 问题。 但是我有一个问题是我错过了将身份验证块添加到请求中。

<soapenv:Header>
      <cis:Authentification>
         <cis:user>user</cis:user>
         <cis:signature>password</cis:signature>
      </cis:Authentification>
 </soapenv:Header>

我尝试添加此块导致 'org.quartz.jobexecutionexception: de.vps.icms.exceptions.icmsscriptingexception: java.lang.noclassdeffounderror: org/apache/axis2/saaj/soapenvelopeimpl'异常。

知道我做错了什么吗? 这里的代码: 公共类 WSClient {

   public WSClient() {
        try {
            GKVAPIServicePortType port = prepareService();
            String b = BWIConstants.SYSPARAM_DHL_WS_URL;
            CreateShipmentOrderRequest createShipmentOrderRequest = new CreateShipmentOrderRequest();
            CreateShipmentOrderResponse createShipmentOrderResponse =
                port.createShipmentOrder(createShipmentOrderRequest);
            createShipmentOrderResponse.getStatus();

        } catch (Exception e) {
            e.printStackTrace();

        }

    }

    private GKVAPIServicePortType prepareService() throws MalformedURLException {
        // get Service stub

        String pathToClassFolder = getClass().getResource("/").toString();
        String fullwsdlFilePath = pathToClassFolder + "/" + "geschaeftskundenversand-api-2.2.wsdl";
        URL wsdlLocation = new URL(fullwsdlFilePath);

        GVAPI20De service = new GVAPI20De(wsdlLocation);

        // get Service Port
        GKVAPIServicePortType port = service.getPort(GKVAPIServicePortType.class);

        // overwrite Endpoint
        Map<String, Object> requestContext = ((BindingProvider) port).getRequestContext();
        requestContext.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, "https://cig.dhl.de/services/sandbox/soap");

        // overwrite BasicAuth Username and Password
        // requestContext.put(BindingProvider.USERNAME_PROPERTY, cigUser);
        // requestContext.put(BindingProvider.PASSWORD_PROPERTY, cigPass);

        // Add authentication Handler
        Binding binding = ((BindingProvider) port).getBinding();
        List<Handler> handlerChain = binding.getHandlerChain();
        handlerChain.add(
            new AuthenticationHandler(BWIConstants.SYSPARAM_DHL_WS_USER, BWIConstants.SYSPARAM_DHL_WS_SIGNATURE));
        binding.setHandlerChain(handlerChain);

        return port;
    }

}

public class AuthenticationHandler implements SOAPHandler<SOAPMessageContext> {

    private String USER = "";
    private String PASSWORD = "";

    public AuthenticationHandler(final String user, final String password) {
        USER = user;
        PASSWORD = password;
    }

    /**
     * {@inheritDoc}
     */
    public void close(final MessageContext context) {
        // nothing to do
    }

    /**
     * {@inheritDoc}
     */
    public Set<QName> getHeaders() {
        // nothing to do
        return null;
    }

    /**
     * {@inheritDoc}
     */
    public boolean handleFault(final SOAPMessageContext context) {
        // nothing to do
        return true;
    }

    /**
     * {@inheritDoc}
     */
    public boolean handleMessage(final SOAPMessageContext context) {
        if (isOutboundMessage(context)) {
            try {
                // get/create the map of HTTP headers
                Map<Object, Object> headers = (Map<Object, Object>) context.get(MessageContext.HTTP_REQUEST_HEADERS);
                if (headers == null) {
                    headers = new HashMap<Object, Object>();
                    context.put(MessageContext.HTTP_REQUEST_HEADERS, headers);
                }

                // add custom HTTP header (deactivate HTTP keepAlive)
                String headerName = "Connection";
                List<String> headerValues = new ArrayList<String>();
                headerValues.add("Close");
                headers.put(headerName, headerValues);

                SOAPMessage message = context.getMessage();
                SOAPEnvelope envelope = message.getSOAPPart().getEnvelope();

                SOAPHeader header;
                if (envelope.getHeader() == null) {
                    header = envelope.addHeader();
                } else {
                    header = envelope.getHeader();
                }

                // add the Authentification element
                SOAPElement auth = header.addHeaderElement(
                    envelope.createName("Authentification", "cis", "http://dhl.de/webservice/cisbase"));
                SOAPElement user =
                    auth.addChildElement(envelope.createName("user", "cis", "http://dhl.de/webservice/cisbase"));
                user.setValue(USER);
                SOAPElement signature =
                    auth.addChildElement(envelope.createName("signature", "cis", "http://dhl.de/webservice/cisbase"));
                signature.setValue(PASSWORD);
                SOAPElement type =
                    auth.addChildElement(envelope.createName("type", "cis", "http://dhl.de/webservice/cisbase"));
                type.setValue("0");

                // save changes
                message.saveChanges();
            } catch (SOAPException ex) {
                throw new RuntimeException("Failed to add SOAP headers for authentication.", ex);
            }
        }
        return true;
    }

    private boolean isOutboundMessage(final MessageContext context) {
        Boolean outboundProperty = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
        return outboundProperty.booleanValue();
    }
}

【讨论】:

    【解决方案2】:

    使用基本身份验证,您首先要对您的用户名:密码进行 Base64 编码 - 有一些在线网站会这样做,但请注意,如果它无论如何都引用 DHL,则这样做可能不是一个好主意,例如他们可以刷你的凭据。
    然后获取端口的请求上下文,创建标头映射并添加授权标头。最后,您将其添加回请求上下文。

    示例: 请注意,我故意生成了错误的 base64 编码,因此您可能无法对其进行解码并使用“用户名:密码”正确格式化

            GVAPI20De service1 = new GVAPI20De();
    
            GKVAPIServicePortType port2 = service1.getGKVAPISOAP11Port0();
    
            CreateShipmentOrderRequest sh = new CreateShipmentOrderRequest();
            //Setting up shipment;
    
            Map<String, Object> req_ctx = ((BindingProvider)port2).getRequestContext();
    
            //you may not need this and can try commenting it out
            req_ctx.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, "https://cig.dhl.de/cig-wsdls/com/dpdhl/wsdl/geschaeftskundenversand-api/2.2/geschaeftskundenversand-api-2.2.wsdl");
    
            //optional timeout
            req_ctx.put("javax.xml.ws.client.connectionTimeout", "60000");
    
            Map<String, List<String>> headers = new HashMap<String, List<String>>();
            headers.put("Authorization", Collections.singletonList("Basic c3gh567sd4689k11lg=="));
    
            req_ctx.put(MessageContext.HTTP_REQUEST_HEADERS, headers);
    
            CreateShipmentOrderResponse chr = port2.createShipmentOrder(sh)
    

    【讨论】:

    • 当我尝试这个时,我得到一个演员错误。 de.dhl.webservices.businesscustomershipping.GKVAPIServicePortTypeProxy 无法转换为 javax.xml.ws.BindingProvider。我也尝试使用 GKVAPIServicePortType cl
    • 您似乎使用了错误的服务?为我生成的客户端代码是不同的。更新了我的答案,添加了正确的服务。
    • 如何导入 WSDL,当我初始化 GVAPI 对象时,他们会在我的项目中创建许多方法,例如“@Override public GKVAPIServicePortType getGKVAPISOAP11port0(URL portAddress) throws ServiceException { // TODO 自动生成方法存根返回 null; } "
    • 我使用 Eclipse IDE 并且还安装了 JBoss,所以我使用 JAX-WS 导入 wsdl 并生成所有存根。我相信你应该使用生成的服务“GVAPI20De”和端口“GKVAPIServicePortType”——你试过看看代码是否有效吗?我没有任何身份验证凭据,因此我无法自己尝试。
    • 这是我的失败,我使用 eclispe + apache 导入 wsdl。现在我使用 Jboss,它的工作很完美。谢谢
    猜你喜欢
    • 2013-01-31
    • 2011-01-24
    • 2011-07-16
    • 2018-12-26
    • 2018-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-30
    相关资源
    最近更新 更多