刚刚在Springmvc项目中出现HTTP Status 405 - HTTP method GET is not supported by this URL错误,然后检查了一下springmvc Controller层,发现这类型的错误是由于你controller层接收的是POST请求,而你客户端发送的是GET请求,所以才会报出GET is not supported错误,即表示不支持get请求。
当然有时候我们有时候也会报如下错误,如:
HTTP Status 405 - HTTP method POST is not supported by this URL错误
这是因为你springmvc controller层接收的GET请求,而你在客户端发送的是POST请求,所以也会报POST is not supported错误。
因此其实这两种错误问题很简单,只需要你检查你客户端发送的请求和springmvc controller层接收的请求方式是否一致即可。
如果是ajax form表单提交的话,请不要把input提交按钮的type设置为submit,否则也会出错的,如下这种形式。
<form> <input class="btn btn-primary radius" type="submit" onclick="checkForm()" value="提交"> </form>
我们可以去掉type="submit",这样也会防止不必要的错误。
总之不管是GET is not supported错误还是POST is not supported错误,都说明你客户端发送的请求方式和你服务器端的controller层或servlet层定义的请求方式不一致造成的。