SpringMVC的REST中错误:HTTP Status 405 - JSPs only permit GET POST or HEAD.

在学习springMVC时 , REST这块PUT和 DELETE 请求时遇到一个问题

Handler当中的方法顺利执行 , 但是 返回页面时却是405 , permit GET POST or HEAD.。

意思就是 它只认识GET , POST , HEAD. 不认识PUT 和 DELETE

原因是tomcat7以上会出现这个问题, 我用的Tomcat9。

解决方法有两种

第一种 

在Controller当中添加 @ResponseBody 或者 @RestController注解: 

但是种最后执行的结果 , 是在页面当中输出方法 return 的内容 


第二种

该方法可以解决响应页面的问题 , 但是却多一些限制

方法也很简单 , 既然不能识别PUT 和 DELETE  , 那就将请求方式改为可以识别的 ,

[java]  view plain  copy
  1. @RequestMapping(value = "/testMethod/{id}" , method = RequestMethod.PUT)  
  2. public String testMethodPUT(@PathVariable(value = "id") Integer id) {  
  3.     System.out.println("testMethodPUT: PUT: "+ id);  
  4.     return "redirect:/success.jsp";
  5. }  

直接重定向到指定jsp页面

也可以重定向到一个 请求处理的方法 , 在由这个方法转发到页面即可

[java]  view plain  copy
  1.     @RequestMapping(value = "/testMethod/{id}" , method = RequestMethod.DELETE)  
  2.     public String testMethodDELETE(@PathVariable(value = "id") Integer id) {  
  3.         System.out.println("testMethod: DELETE: "+ id);  
  4.         return "redirect:/springmvc/success"//重定向到一个没有指定 method的 Handler方法   
  5.     }
  6.       
  7.     @RequestMapping(value = "/success")  
  8.     public String successGenecal() {  
  9.         return "success";  //由该方法 转发到success.jsp页面
  10.    }

至此可以解决 HTTP Status 405 - JSPs only permit GET POST or HEAD 

猜你喜欢

转载自blog.csdn.net/qq_27607965/article/details/80332467
今日推荐