开发者社区> 问答> 正文

SpringMvc中,RequestMethod可以同时支持POST GET访问么??报错

springmvc中,可以用ReuqestMethod属性来控制 POST 或 GET 方式提交表单 或访问url,

//如: 仅GET方式

@RequestMapping(value = "hello",params ="what",method =RequestMethod.GET)

//如: 仅POST方式

@RequestMapping(value = "hello",params ="what",method =RequestMethod.POST)

以上都可以正常工作但是我如果想要同时支持POST和GET,有看到如下写法:

@RequestMapping(value = "hello",params ="what",method ={RequestMethod.POST,RequestMethod.GET})

但是亲测,却报错了,说:

org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver.handleHttpRequestMethodNotSupported(DefaultHandlerExceptionResolver.java:183) org.springframework.web.servlet.PageNotFound
WARN: Request method 'POST' not supported

还有一个疑问就是,如果不写默认是仅支持GET ?仅支持POST?还是既支持GET又支持POST?

@RequestMapping(value = "hello",params ="what")
我觉得貌似是仅支持GET

展开
收起
爱吃鱼的程序员 2020-06-22 22:12:54 1218 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    不写就是同时支持get和post,如果我没记错的话

    @RequestMapping(value="hello",params="what",
    method={RequestMethod.POST,RequestMethod.GET})
    这种写法有人成功过么?
    有哦不写RequestMethod行不行?不可以哦难道真的不可以都支持么晕了~

    <spanstyle="line-height:25.1875px;">默认是GET

    <spanstyle="font-size:10pt;line-height:25.1875px;background-color:#FFFFFF;font-family:'CourierNew';">method={RequestMethod.<spanstyle="font-size:10pt;">POST<spanstyle="font-size:10pt;line-height:25.1875px;background-color:#FFFFFF;font-family:'CourierNew';">,RequestMethod.<spanstyle="font-size:10pt;">GET<spanstyle="font-size:10pt;line-height:25.1875px;background-color:#FFFFFF;font-family:'CourierNew';">}这样写是正确的<spanstyle="line-height:1.5;font-size:10pt;">

    谢谢各位解决了跟我的写法没关系是其他地方出了点错误。。。springmvc乱报错 

    结论:默认不写是既可以GET也可以POST

    不可以同时用GET和POST,除非你重写HTTPServlet中的Service方法。进行逐步实现,但这没有任何意义!

    不写,就是没有任何限制。HTTPMETHOD都支持<divclass="ref">

    引用来自“黑豆白”的答案

    <divclass=ref_body>不写,就是没有任何限制。HTTPMETHOD都支持怎么会呢。我项目里许多地都不写。有些写了。没有问题。404说明是你配置的和你访问的不一致

    <spanstyle="font-size:14px;line-height:normal;">写法没有任何错误,也没有get/post<spanstyle="font-family:微软雅黑,Verdana,sans-serif,宋体;font-size:14px;line-height:normal;background-color:#FFFFFF;">逐步实现一说,当你用Get的时候自然就选择Get了。

    2020-06-22 22:13:10
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载