简体   繁体   English

通过SpringBoot调用REST API时不支持POST异常

[英]POST not supported exception while invoking REST api via SpringBoot

I have a REST API defined in a SpringBoot application and I'm trying to access this via POST using a different application deployed on a Tomcat 8 server. 我在SpringBoot应用程序中定义了REST API,我正尝试使用Tomcat 8服务器上部署的其他应用程序通过POST访问此API。 Upon doing a POST from the other application, I get the following error in the SpringBoot logs: Request method 'POST' not supported. 从其他应用程序执行POST时,SpringBoot日志中出现以下错误:请求方法'POST'不支持。

Here is my Rest Controller class: 这是我的Rest 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";
        }
    }

I could see that a GET request is going through successfully, but I'm not sure what is wrong with the PUT request. 我可以看到一个GET请求正在成功执行,但是我不确定PUT请求出了什么问题。 Also, I am able to invoke the API via POST by using html files within the application that has the REST methods. 另外,我可以通过使用具有REST方法的应用程序中的html文件通过POST调用API。 I am facing this issue while trying to invoke it from a different application. 我尝试从其他应用程序调用它时遇到了这个问题。 Following is the form that I am using: 以下是我使用的表格:

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

Any pointers please? 有指针吗?

It seems to me the same issue for what the solution is provided here 在我看来, 这里提供的解决方案是同一个问题

Add @RequestMapping("YOUR-CONTEXT-VALUE") on the controller's class and remove value from @RestController annotation. 在控制器的类上添加@RequestMapping(“ YOUR-CONTEXT-VALUE”)并从@RestController批注中删除值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM