java - 通过 SpringBoot 调用 REST api 时不支持 POST 异常

标签 java rest tomcat spring-boot

我在 SpringBoot 应用程序中定义了一个 REST API,我正尝试使用部署在 Tomcat 8 服务器上的不同应用程序通过 POST 访问它。从其他应用程序执行 POST 后,我在 SpringBoot 日志中收到以下错误: 不支持请求方法“POST”。

这是我的 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";
        }
    }

我可以看到 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>

有什么建议吗?

最佳答案

在我看来,提供的解决方案是同样的问题here

在 Controller 的类上添加@RequestMapping("YOUR-CONTEXT-VALUE") 并从@RestController 注释中删除值。

关于java - 通过 SpringBoot 调用 REST api 时不支持 POST 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35778594/

相关文章:

java - 使用 Java 查询服务器的时间

java - Selenium, headless 和非 headless 之间切换

Java - 从 File[] 数组中删除一些文件

hibernate - 使用 Jersey 的 JAX-RS 的 hibernate 资源类中具有连接表的多对一

rest - 当用户尝试使用已经存在的用户名注册时,应该发送什么状态码?

Java 枚举在接口(interface)或类中的放置

Java 客户端和服务器应用程序 : Serialization or REST/SOAP/RPC?

java - Spring MVC 应用程序启动时出现奇怪的错误

sql - 在 SQL :query tag; why? JSTL 1.2 内部使用时,使用 AS 重命名 SQL SELECT 语句中的列名称失败

java - 有效 web.xml 中缺少 web-fragment.xml 中的元素