gpt4 book ai didi

java - 更改 servlet 映射会引发 "HTTP Status 405 - Request method ' GET' 不支持”

转载 作者:行者123 更新时间:2023-12-02 04:26:34 25 4
gpt4 key购买 nike

我有一个问题:最初我有这个 servlet 映射:

<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>

Controller 映射到 example.html、example2.html url 时一切都很好。但在某些情况下,我想使用 *.json 映射,对于这种情况,我通过这种方式更改了 servlet 映射:

<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>

经过这次更改,我得到了

HTTP Status 405 - Request method 'GET' not supported

我的 Controller 抛出错误的示例:

       @RequestMapping(value="example.html",method = RequestMethod.GET)
public String example(
@RequestParam(value = "q", defaultValue = "") String query,
@RequestParam(value = "page", required = false, defaultValue = "1") int page,
HttpServletRequest request, Model model) {

String template = "printout-blog";
model.addAttribute("q", query);
return template;
}

最佳答案

您将url-pattern定义为/*,这意味着调度程序将处理所有传入请求。将您的请求映射更改为下一行中的代码片段!(网址应以斜杠 / 开头),一切都会正常工作

 @RequestMapping(value="/example.html",method = RequestMethod.GET)
public String example(
@RequestParam(value = "q", defaultValue = "") String query,
@RequestParam(value = "page", required = false, defaultValue = "1") int page,
HttpServletRequest request, Model model){}

关于java - 更改 servlet 映射会引发 "HTTP Status 405 - Request method ' GET' 不支持”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32066771/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com