java web.servlet.PageNotFound ,不支持请求方法“GET”,Spring

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26605182/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 10:13:29  来源:igfitidea点击:

web.servlet.PageNotFound ,Request method 'GET' not supported,Spring

javaspringspring-mvc

提问by Jaskey

I encounter a strange problem in my Spring MVC Controller.

我在 Spring MVC 控制器中遇到了一个奇怪的问题。

I have four pages in my webapp folder

我的 webapp 文件夹中有四个页面

enter image description here

在此处输入图片说明

@Controller
public class WelcomeController {

    @RequestMapping(value="/wodi/welcome",method=RequestMethod.GET)
    public String welcome(){
        return "redirect:/pages/webwelcome.html";
    }
}

Just now, it worked fine to find the page http://localhost:8080/pages/webwelcome.html, but now I have the error that the browser says:

刚才,它可以很好地找到页面http://localhost:8080/pages/webwelcome.html,但现在我有浏览器说的错误:

There was an unexpected error (type=Method Not Allowed, status=405).
Request method 'GET' not supported

I have no idea what I did that influence it.

我不知道我做了什么影响它。

I read WARN : org.springframework.web.servlet.PageNotFound - Request method 'GET' not supported

我读到警告:org.springframework.web.servlet.PageNotFound - 不支持请求方法“GET”

But this is not the same case as mine since I am using "GET" method.

但这与我的情况不同,因为我使用的是“GET”方法。

Below is my Application.java to Boot the Spring app

下面是我的 Application.java 来启动 Spring 应用程序

@Configuration
@EnableAutoConfiguration
@ComponentScan({"hello","wodinow.weixin.jaskey"})
public class Application {

    public static void main(String[] args) {
        ApplicationContext ctx = SpringApplication.run(Application.class, args);

        System.out.println("Let's inspect the beans provided by Spring Boot:");

        String[] beanNames = ctx.getBeanDefinitionNames();
        Arrays.sort(beanNames);
        for (String beanName : beanNames) {
            System.out.println(beanName);
        }
    }

    @Bean
    public CommandService commandService(){
        return CommandService.getInstance();
    }

}

采纳答案by Serge Ballesta

Unless you put really weird thing in your config, the (embedded) container should be able to serve static content or JSP that are not under WEB-INF. The only use case where problem could happen would be if you map Spring DispatcherServletto /*of forget to allow the serving of static resources.

除非你在你的配置中加入了非常奇怪的东西,否则(嵌入式)容器应该能够提供不在 WEB-INF 下的静态内容或 JSP。可能发生问题的唯一用例是,如果您将 Spring 映射DispatcherServlet/*或忘记允许提供静态资源。

You will find more references on the serving of static resources on my other post Match for root url and serving of static resources.

您会在我的另一篇文章Match for root url and services of static resources 中找到更多关于静态资源服务的参考。

But usually, in a controller you do not redirect to a HTML page, but give the name of a view and the view resolver finds the appropriate view.

但通常,在控制器中,您不会重定向到 HTML 页面,而是提供视图的名称,视图解析器会找到合适的视图。

回答by user3193801

In my case everything was ok. But i have a problem in a controller

就我而言,一切正常。但是我的控制器有问题

that was my problem @RequestMapping( method = RequestMethod.GET)

那是我的问题@RequestMapping(method = RequestMethod.GET)

y change for this:

y 为此更改:

@RequestMapping(value = "/usuario", method = RequestMethod.GET)

and it works

它有效

回答by Jonas

The ViewResolver registered in your application configuration is responsible for resolving pages from a given URL.

在您的应用程序配置中注册的 ViewResolver 负责从给定的 URL 解析页面。

Example: Config for resolving URLs like /welcometo corresponding JSP file /pages/welcome.jsp

示例:用于解析 URL 的配置,例如/welcome对应的 JSP 文件/pages/welcome.jsp

<bean
  class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <property name="viewClass"
    value="org.springframework.web.servlet.view.JstlView" />
  <property name="prefix" value="/pages/" />
  <property name="suffix" value=".jsp" />
</bean>

However, JSP pages are dynamic and need special handling. For static resources like plain html pages, it is sufficient to set up a static mapping for your pages folder.

但是,JSP 页面是动态的,需要特殊处理。对于像纯 html 页面这样的静态资源,为 pages 文件夹设置静态映射就足够了。

<mvc:resources location="/pages/" mapping="/**" />

This would result in all resources in the folder /pages being mapped to URLs starting with "/". For example: /pages/welcome.htmlwould be accessible by http://yourdomain/welcome.html

这将导致文件夹 /pages 中的所有资源都映射到以“/”开头的 URL。例如:/pages/welcome.html可以通过http://yourdomain/welcome.html

And if you want to set up a view resolver for one specific URL you can use a view-controller in the configuration:

如果你想为一个特定的 URL 设置一个视图解析器,你可以在配置中使用一个视图控制器:

<mvc:view-controller path="/wodi/welcome" view-name="/pages/webwelcome.html"/>

UPDATE:

更新:

As you are using Spring Boot with @EnableAutoConfiguration you are already using the second method. Hereyou can see a code snippet from the AutoConfiguration implementation. It shows that a ResourceHandler is added to URL /**with some predefined locations.

当您使用带有 @EnableAutoConfiguration 的 Spring Boot 时,您已经在使用第二种方法。在这里您可以看到来自 AutoConfiguration 实现的代码片段。它表明 ResourceHandler 被添加到/**具有一些预定义位置的URL 。

If you want custom URL mappings, I suggest you use one of the above mentioned methods in a plain Spring MVC configuration. Hereis the documentation for enabling Spring MVC config. You can decide yourself if you use xml or annotation based configuration.

如果您想要自定义 URL 映射,我建议您在普通的 Spring MVC 配置中使用上述方法之一。是启用 Spring MVC 配置的文档。您可以自行决定是使用 xml 还是基于注解的配置。