2011-01-07 89 views
1

嗨,我使用了Spring Web服務這是我的xsd的樣子Spring的web服務的響應具有不同的Web服務(Java)的

<?xml version="1.0" encoding="UTF-8"?> 
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
     xmlns="http://www.test-software.co.uk/abcportal/schema/abcportaltokendetailsws" 
     targetNamespace="http://www.test-software.co.uk/abcportal/schema/abcportaltokendetailsws" 
     elementFormDefault="qualified" attributeFormDefault="unqualified"> 


     <xs:element name="abcPortalTokenDetailsRequest"> 
      <xs:complexType> 
       <xs:annotation> 
         <xs:documentation>The data structure required to get token details</xs:documentation> 
       </xs:annotation> 
       <xs:sequence> 
        <xs:element name="token" type="token3rdParty"/>     
       </xs:sequence> 
      </xs:complexType> 
     </xs:element> 

     <xs:element name="abcPortalTokenDetailsResponse"> 
      <xs:complexType> 
       <xs:sequence> 
        <xs:element name="tokenDetails" type="TokenDetailsType" /> 
        <xs:element name="result" type="resultStructure"/> 
       </xs:sequence> 
      </xs:complexType> 
     </xs:element> 

     <xs:simpleType name="token3rdParty"> 
      <xs:restriction base="xs:token"> 
      <xs:minLength value="1"></xs:minLength> 
      <xs:maxLength value="60"></xs:maxLength> 
      <xs:pattern value="([A-Za-z0-9]+)" /> 
      </xs:restriction> 
     </xs:simpleType> 


     <xs:complexType name="TokenDetailsType"> 
      <xs:annotation> 
       <xs:documentation>token details</xs:documentation> 
      </xs:annotation> 
      <xs:sequence> 
       <xs:element name="sNumber" type="SNumberType"/> 
       <xs:element name="sId" type="SIdType"/> 
       <xs:element name="sName" type="SNameType"/>      
      </xs:sequence> 
     </xs:complexType> 

     <xs:simpleType name="SNumberType"> 
      <xs:restriction base="xs:string"> 
      <xs:minLength value="1"></xs:minLength> 
      <xs:maxLength value="60"></xs:maxLength> 
      <xs:pattern value="([A-Za-z0-9]+)" /> 
      </xs:restriction> 
     </xs:simpleType> 

     <xs:simpleType name="SIdType"> 
      <xs:restriction base="xs:string"> 
      <xs:minLength value="1"></xs:minLength> 
      <xs:maxLength value="2"></xs:maxLength> 
      <xs:pattern value="([A-Za-z]+)" /> 
      </xs:restriction> 
     </xs:simpleType>  

     <xs:simpleType name="SNameType"> 
      <xs:restriction base="xs:string"> 
      <xs:minLength value="1"></xs:minLength> 
      <xs:maxLength value="128"></xs:maxLength> 
      <xs:pattern value="([A-Za-z0-9]+)" /> 
      </xs:restriction> 
     </xs:simpleType>   

     <xs:complexType name="resultStructure"> 
      <xs:sequence> 
       <xs:element name="resultCode" minOccurs="1" maxOccurs="1"> 
        <xs:annotation> 
         <xs:documentation>The result code indicates the outcome of the available list for request.</xs:documentation> 
        </xs:annotation> 
        <xs:simpleType> 
         <xs:restriction base="xs:string"> 
          <xs:enumeration value="OK"/> 
          <xs:enumeration value="INVALID"/> 
          <xs:enumeration value="ERROR"/> 
         </xs:restriction> 
        </xs:simpleType> 
       </xs:element> 
       <xs:element name="resultMessage" minOccurs="1" maxOccurs="1"> 
        <xs:annotation> 
         <xs:documentation>The associated message for this result</xs:documentation> 
        </xs:annotation> 
        <xs:simpleType> 
         <xs:restriction base="xs:string"> 
          <xs:maxLength value="255"/> 
         </xs:restriction> 
        </xs:simpleType> 
       </xs:element> 
      </xs:sequence> 
     </xs:complexType> 

    </xs:schema> 

這裏是端點的多個命名空間

@Endpoint 
public class abcPortalTokenDetailsEndpoint { 

    private IabcPortalManager abcPortalManager; 

    private static final Logger logger = Logger.getLogger(abcPortalTokenDetailsEndpoint.class); 

     public void setAbcPortalManager(IabcPortalManager abcPortalManager) { 
     this.abcPortalManager = abcPortalManager; 
    } 

    @PayloadRoot(localPart= "AbcPortalTokenDetailsRequest", namespace="http://www.test-software.co.uk/abcportal/schema/abcportaltokendetailsws") 
    public abcPortalTokenDetailsResponse doit(abcPortalTokenDetailsRequest request){ 

     abcPortalTokenDetailsResponse response = new abcPortalTokenDetailsResponse();  
     // check that the token is currently valid 
     String token = request.getToken().trim();  
     String resultCode = "ERROR", resultMessage = "Internal error occured - valid response could not be generated"; 
     boolean okResult = true, valid = false;   
     TAuth tp = null; 
     Integer sId = 0; 
     String sName = ""; 
     String errorMsg = null; 

     try { 
      //validate token    
      if (token != null) {     
       resultCode = "OK"; 
       resultMessage = "Valid session"; 
      } else { 
       resultCode = "INVALID"; 
       resultMessage = "No record of user being logged in"; 
       okResult = false; 
      } 

     } catch (DataAccessException ex) {    
      String fmtStr = "Could not determine whether end user token (%s) is valid.\n %s"; 
      String errMsg = String.format(fmtStr, token, ex.getMessage()); 
      okResult = false; 
      logger.error(errMsg);   
      assert(false) : errMsg; 
     } 

     if(okResult){ 
      if(logger.isDebugEnabled()){ 
       logger.debug("abcPortalTokenDetailsResponse Authenticate user"); 
      } 
      tp = abcPortalManager.getTpAuth(token); 

      if(logger.isDebugEnabled()){ 
       if(tp != null){ 

        if (tp.getSId()!= null){ 
        sName = "ct"; abcPortalManager.getSName(tp.getSId()); 
        }    
       } else { 
        logger.debug("abcPortalTokenDetailsResponse tparty details not found"); 
       } 
      } 
      valid = true; 
     } 


     TokenDetailsType tokenDetailsPart = constructResponseTokenDetailsPart(valid, okResult, tp, sName); 
     response.setTokenDetails(tokenDetailsPart); 

     ResultStructure resultPart = constructResponseResultPart(valid, okResult, errorMsg); 
     response.setResult(resultPart); 

     return response; 
    } 




    private TokenDetailsType constructResponseTokenDetailsPart(
      boolean valid, boolean okResult, TAuth tp,String sName) { 

     TokenDetailsType tdt = null; 


     if (valid && okResult) { 

      tdt = new TokenDetailsType();  
      tdt.setSId(tp.getSId()); 
      tdt.setSNumber(tp.getSNumber); 
      tdt.setSName(sName);      

     } 

     return tdt; 

    } 

    /** 
    * 
    * @param response 
    * @param okResult 
    * void 
    * 
    */ 
    private ResultStructure constructResponseResultPart(
      boolean valid, boolean okResult, String errorMessage) { 

     // Determine result part of response 
     String resultCode, resultMessage; 
     if (okResult) { 
      resultCode = (valid) ? "OK" : "INVALID"; 
      resultMessage = (valid) ? "Successfull query" : "User not authorised";   
     } else { 
      resultCode = "ERROR"; 
      resultMessage = "Valid response could not be generated: " + errorMessage; 
     } 
     ResultStructure resp_result = new ResultStructure(); 
     resp_result.setResultCode(resultCode); 
     resp_result.setResultMessage(resultMessage); 

     return resp_result;  

    } 



} 

當我嘗試使用SoapUI測試webservice爲什麼響應在我的項目中有多個不同webservices的命名空間?我期待的只是abcportaltokendetailsws而不是像abcportalloginstatusws這樣的其他服務,abcportallogoutws不知道爲什麼會顯示?

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"> 
    <SOAP-ENV:Header/> 
    <SOAP-ENV:Body> 
     <ns10:abcPortalTokenDetailsResponse 
xmlns:ns10="http://www.test-software.co.uk/abcportal/schema/abcportaltokendetailsws" 
xmlns:ns2="http://www.test-software.co.uk/abcportal/schema/abcportalloginstatusws" xmlns:ns4="http://www.test-software.co.uk/abcportal/schema/abcportallpaymentws" xmlns:ns5="http://www.test-software.co.uk/abcportal/schema/abcportallogoutws" xmlns:ns6="http://www.test-software.co.uk/abcportal/schema/abcportalfundservicews" xmlns:ns7="http://www.test-software.co.uk/abcportal/schema/abcportalpayservicews" 
     <ns10:tokenDetails> 
      <ns10:sNumber>43454</ns10:sNumber> 
      <ns10:sId/> 
      <ns10:sName>Bridge Market</ns10:sName> 
     </ns10:tokenDetails> 
     <ns10:result> 
      <ns10:resultCode>OK</ns10:resultCode> 
      <ns10:resultMessage>Successfull query</ns10:resultMessage> 
     </ns10:result> 
     </ns10:abcPortalTokenDetailsResponse> 
    </SOAP-ENV:Body> 
</SOAP-ENV:Envelope> 
+0

它應該沒關係。是的,這是不整潔的,但它仍然有效。 – skaffman 2011-01-11 10:42:14

+0

避免包含的一些方法。另外,如果我有20個ws,那麼所有的命名空間都沒有意義 – Gauls 2011-01-11 14:55:07

回答

0

這是可能的,以避免使用Spring 2.5與彈簧-WS 1.5.9沿着abcPortalTokenDetailsEndpoint可以延伸MarshallingMethodEndpointAdapter。同樣在配置文件中,如果我們對每個端點有不同的beanid marshallers,它就可以正常工作。我也在Spring 3.0和Spring-ws 1.5.9中嘗試過相同的事情,它不支持多個beanid編組,它需要在bean id中使用marshaller這個詞? 例如 整個問題是使用的contextPath用「:」或「名單」(這增加了所有namesapce在響應)

<oxm:jaxb2-marshaller id="marshaller" contextPath="com.abc.web.ws.login.status.schema:com.abc.web.ws.profile.schema:com.abc.web.ws.tokendetails.schema> 

如果我們改變上面的代碼如下它工作在Spring 2.5.6不是春天3.0爲什麼?

<bean id="abcPortalTokenDetailsEndpoint" class="com.abc.web.ws.tokendetails.abcPortalTokenDetailsEndpoint"> 

     <property name="marshaller" ref="marshaller1"/> 
     <property name="unmarshaller" ref="marshaller1"/> 
    </bean> 

<bean id="abcPortalTrustedAccessLogInEndpoint" class="com.abc.web.ws.login.trustedaccess.abcPortalTrustedAccessLogInEndpoint">  

     <property name="marshaller" ref="marshaller2"/> 
     <property name="unmarshaller" ref="marshaller2"/> 
</bean> 

<bean id="abcPortalThirdPartyLogInEndpoint" class="com.abc.web.ws.login.thirdparty.abcPortalThirdPartyLogInEndpoint"> 

     <property name="marshaller" ref="marshaller"/> 
     <property name="unmarshaller" ref="marshaller"/> 
    </bean> 

    <bean id="marshaller1" class="org.springframework.oxm.jaxb.Jaxb2Marshaller"> 
      <property name="contextPath" value="com.abc.web.ws.tokendetails.schema"/>      
     </bean> 

     <bean id="marshaller2" class="org.springframework.oxm.jaxb.Jaxb2Marshaller"> 
      <property name="contextPath" value="com.abc.web.ws.login.trustedaccess.schema"/>      
     </bean> 

     <bean id="marshaller3" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">   
      <property name="contextPath" value="com.abc.web.ws.login.thirdparty.schema"/>  
     </bean> 









public class abcPortalTokenDetailsEndpoint extends MarshallingMethodEndpointAdapter{ 

    private IabcPortalManager abcPortalManager; 

    private static final Logger logger = Logger.getLogger(abcPortalTokenDetailsEndpoint.class); 

     public void setAbcPortalManager(IabcPortalManager abcPortalManager) { 
     this.abcPortalManager = abcPortalManager; 
    } 




    public abcPortalTokenDetailsResponse handleabcPortalTokenDetailsRequest(abcPortalTokenDetailsRequest request){ 



abcPortalTokenDetailsResponse response = new abcPortalTokenDetailsResponse();  
     // check that the token is currently valid 
     String token = request.getToken().trim();  
     String resultCode = "ERROR", resultMessage = "Internal error occured - valid response could not be generated"; 
     boolean okResult = true, valid = false;   
     TAuth tp = null; 
     Integer sId = 0; 
     String sName = ""; 
     String errorMsg = null; 

     try { 
      //validate token    
      if (token != null) {     
       resultCode = "OK"; 
       resultMessage = "Valid session"; 
      } else { 
       resultCode = "INVALID"; 
       resultMessage = "No record of user being logged in"; 
       okResult = false; 
      } 

     } catch (DataAccessException ex) {    
      String fmtStr = "Could not determine whether end user token (%s) is valid.\n %s"; 
      String errMsg = String.format(fmtStr, token, ex.getMessage()); 
      okResult = false; 
      logger.error(errMsg);   
      assert(false) : errMsg; 
     } 

     if(okResult){ 
      if(logger.isDebugEnabled()){ 
       logger.debug("abcPortalTokenDetailsResponse Authenticate user"); 
      } 
      tp = abcPortalManager.getTpAuth(token); 

      if(logger.isDebugEnabled()){ 
       if(tp != null){ 

        if (tp.getSId()!= null){ 
        sName = "ct"; abcPortalManager.getSName(tp.getSId()); 
        }    
       } else { 
        logger.debug("abcPortalTokenDetailsResponse tparty details not found"); 
       } 
      } 
      valid = true; 
     } 


     TokenDetailsType tokenDetailsPart = constructResponseTokenDetailsPart(valid, okResult, tp, sName); 
     response.setTokenDetails(tokenDetailsPart); 

     ResultStructure resultPart = constructResponseResultPart(valid, okResult, errorMsg); 
     response.setResult(resultPart); 

     return response; 
    } 
0

不需要單獨的編組器。可以通過在Endpoint註釋爲@ResponsePayloadResponse方法解決。 例如:

@PayloadRoot(localPart= "AbcPortalTokenDetailsRequest", namespace="http://www.test-software.co.uk/abcportal/schema/abcportaltokendetailsws") 
@ResponsePayload 

public abcPortalTokenDetailsResponse doit(abcPortalTokenDetailsRequest request){