如何使用具有不同命名空间和相同JAXB类的两个不同端点?

时间:2023-01-22 12:09:17

I am using spring soap ws.

我正在使用春天肥皂。

I have following JAXB domain classes correspond to complex types

我有以下JAXB域类对应复杂类型

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "reference",
    "reason"
})
@XmlRootElement(name = "request-message")
public class RequestMessageType {

    @XmlElement(name = "reference", required = true)
    protected String reference;
    @XmlElement(name = "reason")
    protected String reason;

   // I have getters and setters but removed here.
}

I have following class with @XmlRegistry annotations

我有跟随@XmlRegistry注释的课程

@XmlRegistry
public class ObjectFactory {

    private final static QName _RequestMessage_QNAME = new QName("http://namespace/url", "request-message");

    public ObjectFactory() {
    }

    @XmlElementDecl(namespace = "http://namespace/url", name = "request-message")
    public JAXBElement<RequestMessageType> createDisconnectRequestMessage(RequestMessageType  value) {
        return new JAXBElement<RequestMessageType>(_RequestMessage_QNAME, RequestMessageType.class, null, value);
    }    
}

Following are endpoints

以下是端点

   @Endpoint
    public class FirstEndPoint {

        private static final String NAMESPACE_URI = "http://first/url/version";

        private static final Logger LOG = Logger.getLogger(FirstEndPoint.class);

        @PayloadRoot(namespace = NAMESPACE_URI, localPart = "request-message")
        @ResponsePayload
        public JAXBElement<ResponseMessageType> requestMessage(@RequestPayload JAXBElement<RequestMessageType> requestMessage) {
            LOG.info("request-message : first version ID : " + requestMessage.getValue().getReference());
        //Preparing response and return response 
        }
    }

    @Endpoint
    public class SecondEndPoint {

        private static final String NAMESPACE_URI = "http://second/url/version";
        private static final Logger LOG = Logger.getLogger(SecondEndPoint.class);


        @PayloadRoot(namespace = NAMESPACE_URI, localPart = "request-message")
        @ResponsePayload
        public JAXBElement<ResponseMessageType> requestMessage(@RequestPayload JAXBElement<RequestMessageType> requestMessage) {
            LOG.info("request-message : second version ID : " + requestMessage.getValue().getReference());
         //Preparing response and return response 

        }
    }

When I make Soap request, I am using NAMESPACE_URI given in endpoints in soap request.

当我发出Soap请求时,我在soap请求中使用端点中给出的NAMESPACE_URI。

Here, in this case, I am getting following response

在这种情况下,我得到以下回复

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
    <SOAP-ENV:Header/>
    <SOAP-ENV:Body>
        <SOAP-ENV:Fault>
            <faultcode>SOAP-ENV:Server</faultcode>
            <faultstring xml:lang="en">unexpected element (uri:"http://first/url/version", local:"request-message"). Expected elements are &lt;{http://namespace/url}request-message&gt;</faultstring>
        </SOAP-ENV:Fault>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

If I use "http://namespace/url" as NAMESPACE_URI in endpoint and in soap request I am getting the proper response but I try to make it different for two endpoints with two different namespaces then it is not working and gives above response.

如果我在端点和soap请求中使用“http:// namespace / url”作为NAMESPACE_URI我得到了正确的响应,但我尝试使两个具有两个不同命名空间的端点不同,然后它不起作用并给出上述响应。

How can I use two different namespaces for two different endpoints with same JAXB class? I am completely new to spring and web service.

如何为具有相同JAXB类的两个不同端点使用两个不同的命名空间?我是春天和网络服务的新手。

Additional info : RequestMessageType class and ObjectFactory class are in one package and in package-info.java namespace is

附加信息:RequestMessageType类和ObjectFactory类在一个包中,在package-info.java命名空间中

@javax.xml.bind.annotation.XmlSchema(namespace="http://namespace/url",elementFormDefault=javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
package com.example

Do I need to change anything in package-info.java file ?

我是否需要更改package-info.java文件中的任何内容?

1 个解决方案

#1


5  

I created a sample project. I hope it can be useful to you. You can give a look at it here: https://github.com/angeloimm/spring-ws-sample Basically this is my WSDL file (in SOAP Web Service all is ruled by WSDL):

我创建了一个示例项目。我希望它对你有用。您可以在这里查看它:https://github.com/angeloimm/spring-ws-sample基本上这是我的WSDL文件(在SOAP Web服务中,所有都由WSDL统治):

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
    xmlns:ss="http://www.example.org/SpringSample/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="SpringSample"
    targetNamespace="http://www.example.org/SpringSample/">
    <wsdl:types>
        <xsd:schema targetNamespace="http://www.example.org/SpringSample/">
            <xsd:complexType name="abstractRequest">
                <xsd:sequence minOccurs="1" maxOccurs="1">
                    <xsd:element name="reqName" type="xsd:string" nillable="false"
                        maxOccurs="1" minOccurs="1" />
                </xsd:sequence>
            </xsd:complexType>
            <xsd:complexType name="abstractResponse">
                <xsd:sequence minOccurs="1" maxOccurs="1">
                    <xsd:element name="responseName" type="xsd:string"
                        nillable="false" maxOccurs="1" minOccurs="1" />
                </xsd:sequence>
            </xsd:complexType>
            <xsd:element name="requestImplementation" type="ss:abstractRequest" />
            <xsd:element name="responseImplementation" type="ss:abstractResponse" />
            <xsd:element name="requestImplementation2" type="ss:abstractRequest" />
            <xsd:element name="responseImplementation2" type="ss:abstractResponse" />
        </xsd:schema>
    </wsdl:types>
    <wsdl:message name="OperationRequest">
        <wsdl:part element="ss:requestImplementation" name="request" />
    </wsdl:message>
    <wsdl:message name="OperationResponse">
        <wsdl:part element="ss:responseImplementation" name="response" />
    </wsdl:message>
    <wsdl:message name="OperationRequest2">
        <wsdl:part element="ss:requestImplementation2" name="request2" />
    </wsdl:message>
    <wsdl:message name="OperationResponse2">
        <wsdl:part element="ss:responseImplementation2" name="response2" />
    </wsdl:message>
    <wsdl:portType name="SpringSample">
        <wsdl:operation name="Operation1">
            <wsdl:input message="ss:OperationRequest" />
            <wsdl:output message="ss:OperationResponse" />
        </wsdl:operation>
        <wsdl:operation name="Operation2">
            <wsdl:input message="ss:OperationRequest2" />
            <wsdl:output message="ss:OperationResponse2" />
        </wsdl:operation>
    </wsdl:portType>
    <wsdl:binding name="SpringSampleSOAP" type="ss:SpringSample">
        <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http" />
        <wsdl:operation name="Operation1">
            <soap:operation style="document" soapAction="http://www.example.org/SpringSample/Operation1" />
            <wsdl:input>
                <soap:body use="literal" />
            </wsdl:input>
            <wsdl:output>
                <soap:body use="literal" />
            </wsdl:output>
        </wsdl:operation>
        <wsdl:operation name="Operation2">
            <soap:operation style="document" soapAction="http://www.example.org/SpringSample/Operation2" />
            <wsdl:input>
                <soap:body use="literal" />
            </wsdl:input>
            <wsdl:output>
                <soap:body use="literal" />
            </wsdl:output>
        </wsdl:operation>
    </wsdl:binding> 
    <wsdl:service name="SpringSample">
        <wsdl:port binding="ss:SpringSampleSOAP" name="SpringSampleSOAP">
            <soap:address location="http://www.example.org/" />
        </wsdl:port>
    </wsdl:service>
</wsdl:definitions>

As you can see I defined 2 complex type: abstractRequest and abstractResponse. Then I implemented them by using the elements requestImplementation,requestImplementation2,responseImplementation,responseImplementation2 According to WS-I specification you need to use separate operations and elements

如您所见,我定义了2个复杂类型:abstractRequest和abstractResponse。然后我通过使用元素requestImplementation,requestImplementation2,responseImplementation,responseImplementation2来实现它们。根据WS-I规范,你需要使用单独的操作和元素

Then I wrote this endpoint:

然后我写了这个端点:

@Endpoint
public class SampleEndpoint
{
    private static final Logger logger = LoggerFactory.getLogger(SampleEndpoint.class.getName());
    private static final String NAME_SPACE_URI = "http://www.example.org/SpringSample/";

    @PayloadRoot(namespace = NAME_SPACE_URI, localPart="requestImplementation")
    @ResponsePayload
    public JAXBElement<AbstractResponse> operationOneResp(@RequestPayload JAXBElement<AbstractRequest> ar)
    {
        if( logger.isDebugEnabled() )
        {
            logger.debug("Operation 1 request "+ar.getValue().getReqName());
        }
        ObjectFactory of = new ObjectFactory();
        AbstractResponse aResp = of.createAbstractResponse();
        aResp.setResponseName("operation 1 response");
        JAXBElement<AbstractResponse> result = of.createResponseImplementation(aResp);
        return result;
    }
    @PayloadRoot(namespace = NAME_SPACE_URI, localPart="requestImplementation2")
    @ResponsePayload
    public JAXBElement<AbstractResponse> operationTwoResp(@RequestPayload JAXBElement<AbstractRequest> ar)
    {
        if( logger.isDebugEnabled() )
        {
            logger.debug("Operation 2 request "+ar.getValue().getReqName());
        }
        ObjectFactory of = new ObjectFactory();
        AbstractResponse aResp = of.createAbstractResponse();
        aResp.setResponseName("operation 2 response");
        JAXBElement<AbstractResponse> result = of.createResponseImplementation(aResp);
        return result;
    }
}

As you can see now I always use AbstractRequest and AbstractResponse JAXBElement in both methods. The 2 methods can also be in 2 different endpoints

正如您现在所看到的,我总是在两种方法中使用AbstractRequest和AbstractResponse JAXBElement。这两种方法也可以在两个不同的端点中

I hope it's what you needed and it's useful

我希望这是你需要的,它是有用的

Angelo

安杰洛

#1


5  

I created a sample project. I hope it can be useful to you. You can give a look at it here: https://github.com/angeloimm/spring-ws-sample Basically this is my WSDL file (in SOAP Web Service all is ruled by WSDL):

我创建了一个示例项目。我希望它对你有用。您可以在这里查看它:https://github.com/angeloimm/spring-ws-sample基本上这是我的WSDL文件(在SOAP Web服务中,所有都由WSDL统治):

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
    xmlns:ss="http://www.example.org/SpringSample/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="SpringSample"
    targetNamespace="http://www.example.org/SpringSample/">
    <wsdl:types>
        <xsd:schema targetNamespace="http://www.example.org/SpringSample/">
            <xsd:complexType name="abstractRequest">
                <xsd:sequence minOccurs="1" maxOccurs="1">
                    <xsd:element name="reqName" type="xsd:string" nillable="false"
                        maxOccurs="1" minOccurs="1" />
                </xsd:sequence>
            </xsd:complexType>
            <xsd:complexType name="abstractResponse">
                <xsd:sequence minOccurs="1" maxOccurs="1">
                    <xsd:element name="responseName" type="xsd:string"
                        nillable="false" maxOccurs="1" minOccurs="1" />
                </xsd:sequence>
            </xsd:complexType>
            <xsd:element name="requestImplementation" type="ss:abstractRequest" />
            <xsd:element name="responseImplementation" type="ss:abstractResponse" />
            <xsd:element name="requestImplementation2" type="ss:abstractRequest" />
            <xsd:element name="responseImplementation2" type="ss:abstractResponse" />
        </xsd:schema>
    </wsdl:types>
    <wsdl:message name="OperationRequest">
        <wsdl:part element="ss:requestImplementation" name="request" />
    </wsdl:message>
    <wsdl:message name="OperationResponse">
        <wsdl:part element="ss:responseImplementation" name="response" />
    </wsdl:message>
    <wsdl:message name="OperationRequest2">
        <wsdl:part element="ss:requestImplementation2" name="request2" />
    </wsdl:message>
    <wsdl:message name="OperationResponse2">
        <wsdl:part element="ss:responseImplementation2" name="response2" />
    </wsdl:message>
    <wsdl:portType name="SpringSample">
        <wsdl:operation name="Operation1">
            <wsdl:input message="ss:OperationRequest" />
            <wsdl:output message="ss:OperationResponse" />
        </wsdl:operation>
        <wsdl:operation name="Operation2">
            <wsdl:input message="ss:OperationRequest2" />
            <wsdl:output message="ss:OperationResponse2" />
        </wsdl:operation>
    </wsdl:portType>
    <wsdl:binding name="SpringSampleSOAP" type="ss:SpringSample">
        <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http" />
        <wsdl:operation name="Operation1">
            <soap:operation style="document" soapAction="http://www.example.org/SpringSample/Operation1" />
            <wsdl:input>
                <soap:body use="literal" />
            </wsdl:input>
            <wsdl:output>
                <soap:body use="literal" />
            </wsdl:output>
        </wsdl:operation>
        <wsdl:operation name="Operation2">
            <soap:operation style="document" soapAction="http://www.example.org/SpringSample/Operation2" />
            <wsdl:input>
                <soap:body use="literal" />
            </wsdl:input>
            <wsdl:output>
                <soap:body use="literal" />
            </wsdl:output>
        </wsdl:operation>
    </wsdl:binding> 
    <wsdl:service name="SpringSample">
        <wsdl:port binding="ss:SpringSampleSOAP" name="SpringSampleSOAP">
            <soap:address location="http://www.example.org/" />
        </wsdl:port>
    </wsdl:service>
</wsdl:definitions>

As you can see I defined 2 complex type: abstractRequest and abstractResponse. Then I implemented them by using the elements requestImplementation,requestImplementation2,responseImplementation,responseImplementation2 According to WS-I specification you need to use separate operations and elements

如您所见,我定义了2个复杂类型:abstractRequest和abstractResponse。然后我通过使用元素requestImplementation,requestImplementation2,responseImplementation,responseImplementation2来实现它们。根据WS-I规范,你需要使用单独的操作和元素

Then I wrote this endpoint:

然后我写了这个端点:

@Endpoint
public class SampleEndpoint
{
    private static final Logger logger = LoggerFactory.getLogger(SampleEndpoint.class.getName());
    private static final String NAME_SPACE_URI = "http://www.example.org/SpringSample/";

    @PayloadRoot(namespace = NAME_SPACE_URI, localPart="requestImplementation")
    @ResponsePayload
    public JAXBElement<AbstractResponse> operationOneResp(@RequestPayload JAXBElement<AbstractRequest> ar)
    {
        if( logger.isDebugEnabled() )
        {
            logger.debug("Operation 1 request "+ar.getValue().getReqName());
        }
        ObjectFactory of = new ObjectFactory();
        AbstractResponse aResp = of.createAbstractResponse();
        aResp.setResponseName("operation 1 response");
        JAXBElement<AbstractResponse> result = of.createResponseImplementation(aResp);
        return result;
    }
    @PayloadRoot(namespace = NAME_SPACE_URI, localPart="requestImplementation2")
    @ResponsePayload
    public JAXBElement<AbstractResponse> operationTwoResp(@RequestPayload JAXBElement<AbstractRequest> ar)
    {
        if( logger.isDebugEnabled() )
        {
            logger.debug("Operation 2 request "+ar.getValue().getReqName());
        }
        ObjectFactory of = new ObjectFactory();
        AbstractResponse aResp = of.createAbstractResponse();
        aResp.setResponseName("operation 2 response");
        JAXBElement<AbstractResponse> result = of.createResponseImplementation(aResp);
        return result;
    }
}

As you can see now I always use AbstractRequest and AbstractResponse JAXBElement in both methods. The 2 methods can also be in 2 different endpoints

正如您现在所看到的,我总是在两种方法中使用AbstractRequest和AbstractResponse JAXBElement。这两种方法也可以在两个不同的端点中

I hope it's what you needed and it's useful

我希望这是你需要的,它是有用的

Angelo

安杰洛