我在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>
请指点什么?
答案 0 :(得分:0)
在我看来,提供解决方案的同样问题是here
在控制器的类上添加@RequestMapping(“YOUR-CONTEXT-VALUE”)并从@RestController注释中删除值。