2016-03-03 40 views
0

我在SpringBoot应用程序中定义了REST API,并试图通过POST使用部署在Tomcat 8服务器上的不同应用程序来访问此API。在从其他应用程序执行POST时,SpringBoot日志中出现以下错误: 不支持请求方法“POST”。通过SpringBoot调用REST API时POST不支持异常

这里是我的休息Controller类:

@Controller 
public class RestApiController { 
@RequestMapping(value="/cancel", method=RequestMethod.GET) 
    public @ResponseBody String CancelJobEndPointInfo() { 
     return "A job can be cancelled by POSTing to this URL"; 
    } 

    @RequestMapping(value="/cancel", method=RequestMethod.POST) 
    public @ResponseBody DataPost CancelJobEndPoint(@RequestParam("username") String name, 
      @RequestParam(name = "passPhrase", defaultValue = "null") String passPhrase, 
      @RequestParam("jobnumber") String jobNumber, @RequestParam("file") MultipartFile file){ 
     UserDetails userObject = new UserDetails(); 
     CancelJob job = new CancelJob(); 
     DataPost dpc = new DataPost(); 
     if (!file.isEmpty()) { 
      try { 
       /* 
       * Write private key 
       */ 
       byte[] ppkBytes = file.getBytes(); 
       BufferedOutputStream ppkStream = 
         new BufferedOutputStream(new FileOutputStream(new File(file.getOriginalFilename()))); 
       ppkStream.write(ppkBytes); 
       ppkStream.close(); 

       userObject.setKeyPath(file.getOriginalFilename()); 
       userObject.setUserName(name); 
       userObject.setPassphrase(passPhrase); 

       job.getCancelJob(userObject, jobNumber); 
       dpc.setMessage("Job:" + jobNumber + " Cancelled successfully"); 
       return dpc; 

      } catch (IOException e) { 

       dpc.setMessage("Could not cancel the job:" + jobNumber + " ERROR => " + e.getMessage()); 
       return dpc; 
//    return "Could not cancel the job:" + jobNumber + " ERROR => " + e.getMessage(); 
      } catch (JSchException e){ 
//    return "Could not cancel the job:" + jobNumber + " ERROR => " + e.getMessage(); 
       dpc.setMessage("Could not cancel the job:" + jobNumber + " ERROR => " + e.getMessage()); 
       return dpc; 
      } 
     } else { 
      dpc.setMessage("Private Key file for user: " + name + " is empty"); 
      return dpc; 
//   return "Private Key file for user: " + name + " is empty"; 
     } 
    } 

我看到一个GET请求正在经历成功,但我不知道什么是错的PUT请求。 另外,我可以通过使用具有REST方法的应用程序中的html文件通过POST调用API。 我在尝试从不同的应用程序调用它时遇到此问题。 以下是我正在使用的表格:

    <form action="http://localhost:8080/cancel" enctype="multipart/form-data" 
           method="post" accept-charset="utf-8" id="jobCancelForm" 
           name="job"> 
           <h4 class="section-heading">User Details:</h4> 
           <fieldset class="form-group"> 
            <label for="UserID">User Name</label> <input type="username" 
             class="form-control" id="canceluser" 
             placeholder="Enter SSH User ID" name="username"> 
           </fieldset> 
           <fieldset class="form-group"> 
            <label for="passPhrase">PassPhrase :</label> <input 
             type="password" class="form-control" id="cancelpass" 
             placeholder="Private Key PassPhrase" name="passPhrase"> 
           </fieldset> 
           <fieldset class="form-group"> 
            <label for="PrivateKeyFileInput">Private Key File</label> <input 
             type="file" class="form-control-file" 
             id="cancelPrivateKeyFileInput" name="cFile"> <small 
             class="text-muted">Select the pre-configured private 
             key file to be used for SSH authentication.</small> 
           </fieldset> 
           <a role="separator" class="divider"></a> 
           <hr> 
           <h4 class="section-heading">Job Details:</h4> 
           <fieldset class="form-group"> 
            <label for="jobID">Job ID :</label> <input type="number" 
             class="form-control" id="canceljobID" 
             placeholder="Enter the job ID Ex: 1242254" name="jobNumber"> 
           </fieldset> 

           <button type="submit" class="btn btn-primary" 
            id="jobMonitorButton">Cancel Job</button> 

          </form> 
          <a role="separator" class="divider"></a> 
          <!-- response div --> 
          <div style="word-wrap: break-word;" id="cancelResponse"></div> 
         </div> 
         <div class="modal-footer"> 
          <button type="button" class="btn btn-default" 
           data-dismiss="modal" id="jobCancelFormCloseButton">Close</button> 
         </div> 
        </div> 

任何指针请?

+0

你可以发布你的要求吗? – Abdelhak

+0

对不起,但你能告诉我如何获得XML请求吗?如果有帮助,我已经添加了我用来调用API的表单html。 – seriousgeek

回答

0

在我看来,什么该解决方案提供here

添加@RequestMapping同样的问题(“您的上下文价值”)的控制器类和@RestController注释删除值。