No mapping found for HTTP request with URI (spring 4.1 annotation configuration)

北战南征 提交于 2019-12-08 04:49:28

Given that you're using Spring Boot, your configuration is far more complicated than it needs to be. Boot will automatically configure the DispatcherServlet, ViewResolver, RequestMappingHandlerMapping, and RequestMappingHandlerAdapter for you.

Take a look at Spring Boot's JSP sample to see how little configuration you need.

In addition to removing pretty much all of the manual configuration, one key difference is that your main application class should extend SpringBootServletInitializer rather than implementing Spring's WebApplicationInitializer directly. Here's the equivalent class in the aforementioned sample:

package sample.jsp;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableAutoConfiguration
@ComponentScan
public class SampleWebJspApplication extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(SampleWebJspApplication.class);
    }

    public static void main(String[] args) throws Exception {
        SpringApplication.run(SampleWebJspApplication.class, args);
    }

}

The main method is used when launched the war file using java -jar and the configure method is used when the war is deployed to a separate Servlet container.

Also note the sample's use of application.properties to configure the view prefix and suffix.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!