2017-08-09 90 views
10

Yay sabunu ws kullanıyorum. JAXB etki alanı sınıfları şu varFarklı ad alanı ve aynı JAXB sınıfı ile iki farklı son noktaya nasıl sahip olabilirim?

@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); 
    }  
} 

ardından karmaşık tip

@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 @XmlRegistry ek açıklamaları ile aşağıdaki sınıfı I sabun hale bitiş noktaları

@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 

     } 
    } 

tekabül istek, sabun isteğinde uç noktalarda verilen NAMESPACE_URI kullanıyorum. Ben son noktada NAMESPACE_URI olarak " http://namespace/url" kullanmak ve sabun istekte düzgün bir yanıt alıyorum ama diğerlerinden farklı olmasını çalışırsanız

İşte

, bu durumda, ben tepki

<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> 

aşağıdaki alıyorum İki farklı isim aralığına sahip iki uç nokta daha sonra çalışmıyor ve yukarıdaki yanıtı veriyor.

Aynı JAXB sınıfına sahip iki farklı uç nokta için iki farklı ad alanını nasıl kullanabilirim? Ben bahar ve web servisine tamamen yeniyim.

Ek bilgi: Bir pakette ve package-info.java ad alanında RequestMessageType sınıf ve ObjectFactory sınıfı vardır

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

ben package-info.java dosyada bir değişiklik gerekiyor mu nedir?

+0

ben' benim şemalar herhangi bir durumda da üretilen WSDL görmek iyi olurdu d iki istek ve cevap öğesini tanımlar. Bu elemanlar aynı tipte olacaktır. Eğer zamanım varsa, github hesabımdaki basit bir projeyi paylaşabilirim –

+0

Bir RequestMessageType özeti hazırlamanızı ve ondan genişletmek için 2 sınıf yapmayı öneririm. Bu sınıflar ayrı ayrı kaydedilir.Bunu yaparak: wsdls'leriniz farklı ad alanlarına sahip olur, ancak servis katmanınız değiştirilemez. –

cevap

5

Örnek bir proje oluşturdum. Umarım sizin için yararlı olabilir. Temelde bu benim WSDL dosyası (SOAP Web Servisi tüm WSDL tarafından yönetiliyor) olduğu https://github.com/angeloimm/spring-ws-sample :

<?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> 

ben 2 karmaşık türü tanımlanmış görebileceğiniz gibi: abstractRequest ve abstractResponse Burada ona bir göz verebilir. Size şöyle

@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; 
    } 
} 

: Sonra Ben bu son nokta yazdım ayrı operasyonlar ve unsurların Sonra

kullanmak gerekir WS-ı şartnamesine göre, responseImplementation2 elemanları requestImplementation, requestImplementation2, responseImplementation kullanarak bunları uygulamaya Şimdi görebiliyorum her zaman iki yöntemde de AbstractRequest ve AbstractResponse JAXBElement kullanıyorum. 2 yöntem ayrıca 2 farklı uç noktaları

Ben size gereken ve

Angelo yararlı olan budur umut olabilir

İlgili konular