首页 > 解决方案 > Web 服务不接受在 Glassfish 上指定命名空间的 xml 元素

问题描述

我正在将 Web 服务从 Weblogic 迁移到 Glassfish,并且遇到了非常奇怪的行为。以前,当我将此 xml 发送到服务时,它被正确解析:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:NS1="http://croc/WS_get_ota_info_for_K2.wsdl">
    <soapenv:Body>
        <NS1:courses >
            <NS1:String_1>filter value</NS1:String_1>
        </NS1:courses>
    </soapenv:Body>
</soapenv:Envelope>

但是在我迁移到 Glassfish 4.1.2 之后,元素 String_1 的值没有用作方法参数。传递了 Null 而不是“过滤器值”。但是当我将 xml 更改为此

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:NS1="http://croc/WS_get_ota_info_for_K2.wsdl">
    <soapenv:Body>
        <NS1:courses >
            <String_1>filter value</String_1>
        </NS1:courses>
    </soapenv:Body>
</soapenv:Envelope>

一切恢复正常。此 xml 对在 weblogic 上工作的以前版本的服务也有效。

这种行为的原因是什么?WSDL 没有改变,所以我希望我的服务在两个 Web 服务器上以相同的方式工作。

这是我的 WSDL:

<?xml version="1.0" encoding="UTF-8" ?>
<wsdl:definitions
     name="WS_get_ota_info_for_K2"
     targetNamespace="http://croc/WS_get_ota_info_for_K2.wsdl"
     xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
     xmlns:tns="http://croc/WS_get_ota_info_for_K2.wsdl"
     xmlns:xsd="http://www.w3.org/2001/XMLSchema"
     xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/"
     xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
     xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/">
    <wsdl:types>
    </wsdl:types>
    <wsdl:message name="WS_get_ota_info_for_K2_courses">
        <wsdl:part name="String_1" type="xsd:string"/>
    </wsdl:message>
    <wsdl:message name="WS_get_ota_info_for_K2_coursesResponse">
        <wsdl:part name="result" type="xsd:string"/>
    </wsdl:message>
    <wsdl:portType name="WS_get_ota_info_for_K2">
        <wsdl:operation name="courses" parameterOrder="String_1">
            <wsdl:input message="tns:WS_get_ota_info_for_K2_courses"/>
            <wsdl:output message="tns:WS_get_ota_info_for_K2_coursesResponse"/>
        </wsdl:operation>
    </wsdl:portType>
    <wsdl:binding name="WS_get_ota_info_for_K2" type="tns:WS_get_ota_info_for_K2">
        <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
        <wsdl:operation name="courses">
            <soap:operation soapAction="http://croc/WS_get_ota_info_for_K2.wsdl/courses"/>
            <wsdl:input>
                <soap:body use="literal" namespace="http://croc/WS_get_ota_info_for_K2.wsdl" parts="String_1"/>
            </wsdl:input>
            <wsdl:output>
                <soap:body use="literal" namespace="http://croc/WS_get_ota_info_for_K2.wsdl" parts="result"/>
            </wsdl:output>
        </wsdl:operation>
    </wsdl:binding>
    <wsdl:service name="WS_get_ota_info_for_K2">
        <wsdl:port name="WS_get_ota_info_for_K2Port" binding="tns:WS_get_ota_info_for_K2">
            <soap:address location="http://localhost:7101/WS_get_ota_info_for_K2/WS_get_ota_info_for_K2Port"/>
        </wsdl:port>
    </wsdl:service>
</wsdl:definitions>

和 Java 类

import java.sql.CallableStatement;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Types;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import javax.sql.DataSource;

@SOAPBinding(style = SOAPBinding.Style.RPC)
@WebService(name = "WS_get_ota_info_for_K2", serviceName = "WS_get_ota_info_for_K2",
            targetNamespace = "http://croc/WS_get_ota_info_for_K2.wsdl", portName = "WS_get_ota_info_for_K2Port",
            wsdlLocation = "WEB-INF/wsdl/WS_get_ota_info_for_K2.wsdl")
public class WSGetOtaInfoForK2Impl {
    private static final String DATA_SOURCE = "jdbc/mainDS";
    protected DataSource __dataSource = null;

    public WSGetOtaInfoForK2Impl() throws NamingException {
        Context ctx = new InitialContext();
        __dataSource = (DataSource) ctx.lookup(DATA_SOURCE);
    }

    @WebResult(name = "result", partName = "result")
    @WebMethod(action = "http://croc/WS_get_ota_info_for_K2.wsdl/courses")
    public String courses(@WebParam(name = "String_1", partName = "String_1") String string1) throws SQLException {
        String response = calculateSomething(string1);
        return response;
    }
}

标签: javaxmlsoapglassfish

解决方案


推荐阅读