2016-07-28 62 views
0

我工作的一個春天RESTful Web服務,其中我返回XML文件作爲響應發送。這個XML文件放在eclipse中MAVEN項目的main/resources文件夾中。該服務接受調用者的某些參數,並根據這些參數更新XML文件。該項目在生產服務器中作爲WAR進行部署。在我的本地項目中,我可以看到正在更新的xml文件,但在生產服務器中不是。我怎樣才能在生產服務器上工作?Spring REST Web服務|更新XML和響應

下面是控制器接受傳入請求

@RestController 
public class HelloWorldRestController { 

@Autowired 
UserService userService; // Service which will do all data 
          // retrieval/manipulation work 

@Autowired 
DialogServiceXml dialogService; 

// Returning an xml file in the response 
@CrossOrigin(origins = "*") 
@RequestMapping(value = "/getUpdatedDialog", method = RequestMethod.POST, produces = "application/xml") 
public ResponseEntity<InputStreamResource> downloadXMLFile(@RequestBody Dialog dialog, 
     UriComponentsBuilder ucBuilder) throws IOException { 

    // Update the xml file named : "mor_dialog.xml" 
    dialogService.updateXml(new StringBuilder(dialog.getClassName()), new StringBuilder(dialog.getResponse())); 

    // Pick up the updated file from the classpath 
    ClassPathResource xmlFile = null; 
    try { 
     xmlFile = new ClassPathResource("mor_dialog.xml"); 
    } catch (Exception exception) { 
     exception.printStackTrace(); 
    } 

    // Code to prevent caching so that always the latest version of the file 
    // is being sent. 
    HttpHeaders httpHeaders = new HttpHeaders(); 
    httpHeaders.add("Cache-Control", "no-cache, np-store, must-revalidate"); 
    httpHeaders.add("Pragma", "no-cache"); 
    httpHeaders.add("Expires", "0"); 
    //httpHeaders.add("Access-Control-Allow-Origin", "http://nlc-mor-furniture.mybluemix.net"); 

    return ResponseEntity.ok().headers(httpHeaders).contentLength(xmlFile.contentLength()) 
      .contentType(MediaType.parseMediaType("application/octet-stream")) 
      .body(new InputStreamResource(xmlFile.getInputStream())); 
    } 
} 

下面的代碼是解組XML中的類,更新它基於輸入參數,然後編組回

@Service("dialogService") 
public class DialogServiceXml implements DialogServiceXmlImpl { 

@SuppressWarnings({ "rawtypes", "unchecked" }) 
@Override 
public void updateXml(StringBuilder className, StringBuilder response) { 
    JAXBContext jaxbContext = null; 
    ClassLoader classLoader = getClass().getClassLoader(); 
    try { 
     jaxbContext = JAXBContext.newInstance("com.sau.watson.dialog.xsd.beans"); 

     Unmarshaller JAXBUnmarshaller = jaxbContext.createUnmarshaller(); 

     Marshaller JAXBMarshaller = jaxbContext.createMarshaller(); 
     JAXBMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); 
     JAXBMarshaller.setProperty(Marshaller.JAXB_NO_NAMESPACE_SCHEMA_LOCATION, "WatsonDialogDocument_1.0.xsd"); 

     File mor_dialog = new File(classLoader.getResource("mor_dialog.xml").getFile()); 
     //File mor_dialog = new File(classLoader.getResource("../../mor_dialog.xml").getFile()); 

     mor_dialog.setWritable(true); 
     //File mor_dialog_updated = new File(classLoader.getResource("mor_dialog_updated.xml").getFile()); 

     InputStream is = new FileInputStream(mor_dialog); 

     JAXBElement dialog = (JAXBElement) JAXBUnmarshaller.unmarshal(is); 
     is.close(); 

     //JAXBElement dialog = (JAXBElement) JAXBUnmarshaller.unmarshal(new FileInputStream("src/main/java/com/sau/watson/dialog/xml/mor_dialog.xml")); 

     DialogType dialogType = (DialogType) dialog.getValue(); 
     // System.out.println(dialogType.toString()); 
     // System.out.println(dialogType); 

     FlowType flowType = (FlowType) dialogType.getFlow(); 

     for (FolderNodeType folderNodeType : flowType.getFolder()) { 
      // System.out.println(folderNodeType.getLabel()); 

      if (folderNodeType.getLabel().equalsIgnoreCase("Library")) { 

       for (ChatflowNode libChatFlowNode : folderNodeType.getInputOrOutputOrDefault()) { 
        FolderNodeType libraryFolderNode = (FolderNodeType) libChatFlowNode; 
        // System.out.println(libraryFolderNode.getId()); 
        // System.out.println(libraryFolderNode.getLabel()); 

        StringBuilder classNameFromXml = new StringBuilder(); 

        for (ChatflowNode node : libraryFolderNode.getInputOrOutputOrDefault()) { 
         InputNodeType inputNodeType = (InputNodeType) node; 

         // Getting the class. Class name are encapsulated 
         // inside the <grammar> node 
         /** 
         * <grammar> <item>Salesperson_Great</item> 
         * <item>Great</item> </grammar> 
         */ 
         for (Object grammerTypeObj : inputNodeType.getActionOrScriptOrGrammar()) { 
          GrammarType grammarType = (GrammarType) grammerTypeObj; 

          // We are always getting the first item as it is 
          // assumed that there is only one class in each 
          // grammar node 
          classNameFromXml 
            .append(grammarType.getItemOrSourceOrParam().get(0).getValue().toString()); 
          System.out.println("Class Name is : " + className); 
         } 

         // We are always getting the first item as it is 
         // assumed that there is only one class in each 
         // grammar node 
         /* 
         * List<Object> grammarTypeObj = 
         * inputNodeType.getActionOrScriptOrGrammar(); 
         * GrammarType grammarType = (GrammarType) 
         * grammarTypeObj; 
         * 
         * String className = 
         * grammarType.getItemOrSourceOrParam().get(0). 
         * getValue().toString(); 
         * 
         * System.out.println("Class Name is : "+className); 
         */ 

         if (!classNameFromXml.toString().equalsIgnoreCase(className.toString())) { 
          continue; 
         } 

         // Getting all the response items corresponding to 
         // this class 
         for (ChatflowNode outputNodeObj : inputNodeType.getInputOrOutputOrDefault()) { 

          OutputNodeType outputNode = (OutputNodeType) outputNodeObj; 
          for (Object promptTypeObj : outputNode.getActionOrScriptOrPrompt()) { 

           PromptType promptType = (PromptType) promptTypeObj; 

           List<Serializable> responseItemObjs = promptType.getContent(); 
           for (Object responseItemObj : responseItemObjs) { 

            /* 
            * if (responseItemObj instanceof 
            * String) { 
            * System.out.println(((String) 
            * responseItemObj).trim()); } 
            */ 
            if (responseItemObj instanceof JAXBElement) { 
             // System.out.println("JAXBElement 
             // Instance"); 

             JAXBElement responseItem = (JAXBElement) responseItemObj; 

             System.out.println("The old response is : " + responseItem.getValue().toString()); 
             responseItem.setValue(response.toString()); 
            } 
           } 
          } 
         } 
        } 
       } 
      } 
     } 

     FileOutputStream os = new FileOutputStream(mor_dialog); 

     JAXBMarshaller.marshal(dialog, os);    
     //os.flush(); 
     os.close(); 

     //JAXBMarshaller.marshal(dialog, new FileOutputStream("src/main/java/com/sau/watson/dialog/xml/mor_dialog.xml")); 

    } catch (Exception ex) { 
     ex.printStackTrace(); 
    } 
} 


} 
+0

任何代碼,服務器日誌的輸出將需要任何建議/解決方案。你可以提供他們請 –

+0

添加我正在處理的代碼。謝謝 –

回答

0

的在maven構建war文件之前,src/main/resources文件夾僅在您的開發機器上可用。構建戰爭後,資源將從類路徑的根目錄添加到war文件。 如果你想更新文件,那麼你可能想要從文件系統而不是從類路徑訪問文件。