2017-08-31 15 views
0

私はApexのhttpポストに応答してXMLを返そうとしています。私の人生では、どうやってそれを行うのか理解できません。私は現在持っている:ApexのHTTPポストに応答してXMLを返すには?

@RestResource(urlMapping='/routeAPIs/*') 
global class routeAPIController { 
    @HttpPost 
    global static String getOwner(String interation_id, String source_address, String destination_address) { 


     //Get day of the week to check for weekend 
     Boolean dayFlag = false; 
     Date myDate = System.today(); 
     DateTime myDateTime = (DateTime) myDate; 
     String dayOfWeek = myDateTime.format('E'); 
     if(dayOfWeek == 'Sat' || dayOfWeek == 'Sun'){ 
      dayFlag = true; 
     } 

     try{ 
      //Query for the owner using the case number entered 
      Case A = [SELECT OwnerId, Status, CaseNumber FROM Case WHERE Case.ContactPhone =: source_address limit 1]; 
      //Convert OwnerId to string 
      String caseOwner = String.valueOf(A.OwnerId); 
      //Query for the email of the user using the case owner ID 
      User B = [Select Email From User where id = : caseOwner limit 1]; 
      //Convert email to string 
      String ownerEmail = String.valueOf(B.Email); 
      //return xml for successful find of case and owner 
      //Checks for weekend, else weekday output 
      if(dayFlag){ 
       //Checks if most recent case is closed, else case is open 
       if(A.Status == 'Closed'){ 
        String xml = '<?xml version="1.0" encoding="UTF-8"?><Response><Context output="closed"><Fields><Field name="ringGroup">weekendResponder</Field></Fields></Context></Response>'; 
        return xml; 
       } 
       else{ 
        String xml = '<?xml version="1.0" encoding="UTF-8"?><Response><Context output="open"><Fields><Field name="email">' + ownerEmail + '</Field><Field name="ringGroup">weekendResponder</Field></Fields></Context></Response>'; 
        return xml; 
       } 
      } 
      else{ 
       //Checks if most recent case is closed, else case is open 
       if(A.Status == 'Closed'){ 
        String xml = '<?xml version="1.0" encoding="UTF-8"?><Response><Context output="closed"><Fields><Field name="ringGroup">trafficCop</Field></Fields></Context></Response>'; 
        return xml; 
       } 
       else{ 
        String xml = '<?xml version="1.0" encoding="UTF-8"?><Response><Context output="open"><Fields><Field name="email">' + ownerEmail + '</Field><Field name="ringGroup">trafficCop</Field></Fields></Context></Response>'; 
        return xml; 
       } 
      } 
     } 
     //If case isn't found or not enough numbers were entered 
     catch(QueryException e){ 
      //If no case is found - Routes to weekendResponder/trafficCop depending on day 
      if(dayFlag){ 
       String xml = '<?xml version="1.0" encoding="UTF-8"?><Response><Context output="newNum"><Fields><Field name="ringGroup">weekendResponder</Field></Fields></Context></Response>'; 
       return xml; 
      } 
      else{ 
       String xml = '<?xml version="1.0" encoding="UTF-8"?><Response><Context output="newNum"><Fields><Field name="ringGroup">trafficCop</Field></Fields></Context></Response>'; 
       return xml; 
      } 
     } 

     //So end of function can't be reached - Routes to weekendResponder/trafficCop depending on day 
     if(dayFlag){ 
       String xml = '<?xml version="1.0" encoding="UTF-8"?><Response><Context output="wrong"><Fields><Field name="ringGroup">weekendResponder</Field></Fields></Context></Response>'; 
       return xml; 
      } 
      else{ 
       String xml = '<?xml version="1.0" encoding="UTF-8"?><Response><Context output="wrong"><Fields><Field name="ringGroup">trafficCop</Field></Fields></Context></Response>'; 
       return xml; 
      } 
     } 
} 

現在のコードの問題は、それが応答が引用符で囲まれているし、適切にXMLとして読み込まれていないので、文字列を返します。私はxmlのドキュメントを構築し、RestResponseとして文字列を入れてみましたが、私はそれを保存しようとするとエラー "コンパイルエラー:無効なタイプのHttp *メソッド:.."を得ました。

答えて

0

Thisは、XMLを解析する良い例です。文字列形式で返すには、まずDOMに変換して子要素を追加し、toXmlString()を使用してXMLを文字列に変換する必要があります。 Thisあなたにそれを変換する例を教えてください

public String toXml() { 

    // Create our top level doc and request node 
    Dom.Document requestDoc = createXMLRequestDocument(); 
    Dom.XmlNode bodyNode = requestDoc.getRootElement().addChildElement('soapenv:Body', null, null); 
    Dom.XmlNode requestMessageNode = bodyNode.addChildElement('addressValidationRequest', null, null); 

    // Add user details 
    Dom.XmlNode userDetailsNode = requestMessageNode.addChildElement('userDetails', null, null); 
    userDetails.addToXmlDoc(userDetailsNode); 

    // Add address 
    Dom.XmlNode addressNode = requestMessageNode.addChildElement('address', null, null); 
    address.addToXmlDoc(addressNode); 

    return requestDoc.toXMLString(); 
} 
関連する問題