SpringMVC: HTTP Status 405 - JSPs only permit GET POST or HEAD问题的解决办法

这位大佬写的:传送门

1.出错时的代码
web.xml:

HiddenHttpMethodFilter org.springframework.web.filter.HiddenHttpMethodFilter HiddenHttpMethodFilter /* 1 2 3 4 5 6 7 8 9 JSP: 1 2 3 4 Controller:

@RequestMapping("/springmvc")
@Controller
public class SpringMVCTest {
private static final String SUCCESS = “success”;
@RequestMapping(value="/testRest/{id}",
method=RequestMethod.PUT)
public String testRestPUT(@PathVariable(value=“id”)
Integer id) {
System.out.println("testRest PUT: " + id);
return SUCCESS;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
视图解析器:

1 2 3 4 2.出错的原因 发起的请求是个RESTFul风格的请求,调用了RESTFul风格的PUT方法。但是controller里testRestPUT返回的success字符串被映射到success.jsp。因此spring认为这应该是个JSP接口,且JSP接口仅仅支持GET方法和POST方法。所以系统提示提示了这个错误。

3.解决方法
为controller里的testRestPUT方法加上@ResponseBody()注解,并且返回一个字符串。关键代码如下:

@RequestMapping(value="/testRest/{id}", method=RequestMethod.PUT)
@ResponseBody()
public String testRestPUT(@PathVariable(value=“id”)
Integer id) {
System.out.println("testRest PUT: " + id);
return “abc”;
}
1
2
3
4
5
6
7
系统会返回字符串abc。

你可能感兴趣的:(springmvc)