0

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. Upon doing a POST from the other application, I get the following error in the SpringBoot logs: Request method 'POST' not supported.

Here is my Rest Controller class:

   @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. Also, I am able to invoke the API via POST by using html files within the application that has the REST methods. 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?

seriousgeek
  • 992
  • 3
  • 13
  • 29

1 Answers1

0

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.

Community
  • 1
  • 1