Java Spring Boot:servlet [dispatcherServlet]的Servlet.service()引发了异常

时间:2017-07-14 23:23:14

标签: java angularjs spring spring-mvc

我正在尝试编写一个Spring Boot应用程序来为我的AngularJS应用程序提供单个文件“index.html”。我有以下课程:

Application.java

@SpringBootApplication
public class Application
        extends SpringBootServletInitializer
{
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application)
    {
        return application.sources(Application.class);
    }

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

BaseController.java

@Controller
public class BaseController
{
    // Map all urls to this method aside from ones starting with api
    @RequestMapping(value = "/**")
    public String getIndexHtml()
    {
        System.out.println("hi me");
        return "index";
    }
}

MvcConfiguration.java

@Configuration
public class MvcConfiguration
        extends WebMvcConfigurerAdapter
{
    @Bean
    public ViewResolver getViewResolver()
    {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/");
        resolver.setSuffix(".html");
        return resolver;
    }
}

以下目录结构:

src
 └── main
     ├── java
     │   └── com
     │       └── myangularapp
     │           ├── Application.java
     │           ├── config
     │           │   └── MvcConfiguration.java
     │           └── controllers
     │               └── BaseController.java
     └── webapp
         └── WEB-INF
             └── index.html

然后我在gracat上部署之前将gradle创建应用程序。

但是,每当我尝试访问该网站时,我都会看到此页面:enter image description here

以下堆栈跟踪:

2017-07-15 00:01:51.551 ERROR 42496 --- [nio-8090-exec-1] o.a.c.c.C.[.[.[.5.0.[dispatcherServlet]  : Servlet.service() for servlet [dispatcherServlet] threw exception

java.lang.StackOverflowError: null
at javax.servlet.http.HttpServletRequestWrapper.getSession(HttpServletRequestWrapper.java:231) ~[servlet-api.jar:na]
at org.apache.catalina.core.ApplicationHttpRequest.getSession(ApplicationHttpRequest.java:594) ~[catalina.jar:8.5.15]
at javax.servlet.http.HttpServletRequestWrapper.getSession(HttpServletRequestWrapper.java:231) ~[servlet-api.jar:na]

最终变成:

at javax.servlet.http.HttpServletRequestWrapper.getSession(HttpServletRequestWrapper.java:231) ~[servlet-api.jar:na]
at org.apache.catalina.core.ApplicationHttpRequest.getSession(ApplicationHttpRequest.java:594) ~[catalina.jar:8.5.15]
at org.springframework.web.util.WebUtils.getSessionId(WebUtils.java:288) ~[spring-web-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.publishRequestHandledEvent(FrameworkServlet.java:1077) ~[spring-webmvc-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005) ~[spring-webmvc-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861) ~[spring-webmvc-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:635) ~[servlet-api.jar:na]
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846) ~[spring-webmvc-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:742) ~[servlet-api.jar:na]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) ~[catalina.jar:8.5.15]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[catalina.jar:8.5.15]
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) ~[tomcat-websocket.jar:8.5.15]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[catalina.jar:8.5.15]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[catalina.jar:8.5.15]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:101) ~[spring-web-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[catalina.jar:8.5.15]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[catalina.jar:8.5.15]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:101) ~[spring-web-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[catalina.jar:8.5.15]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[catalina.jar:8.5.15]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:101) ~[spring-web-4.3.7.RELEASE.jar:4.3.7.RELEASE]

有人知道我做错了吗?

------------------------的更新 --------------- ---------

answer的后面,我的MvcConfiguration课上没有@EnableWebMvc。我添加了它,视图已经解决,一切都开始工作了。

1 个答案:

答案 0 :(得分:-1)

Spring Boot将自动添加位于以下任何目录中的静态Web资源:

  
      
  • / META-INF /资源/
  •   
  • /资源/
  •   
  • /静态/
  •   
  • /公共/
  •   

您需要创建一些春天想要的目录,创建一个 src/main/resources/index.html

此致