简体   繁体   English

Java Spring Boot:Servlet [dispatcherServlet]的Servlet.service()抛出异常

[英]Java Spring Boot: Servlet.service() for servlet [dispatcherServlet] threw exception

I am trying to write a Spring Boot application to serve a single file, "index.html", for my AngularJS application. 我正在尝试编写一个Spring Boot应用程序来为AngularJS应用程序提供一个文件“ index.html”。 I have the following classes: 我有以下课程:

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 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 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;
    }
}

And the following directory structure: 以及以下目录结构:

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

I then have gradle create the application into a war before deploying on tomcat 8. 然后,在将其部署到tomcat 8上之前,我让gradle创建了应用程序。

However, any time I try and visit the website, I always see this page: 但是,每次尝试访问该网站时,我总是会看到此页面: 在此处输入图片说明

And the following stack trace: 和以下堆栈跟踪:

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]

Which eventually turns into: 最终变成:

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]

Does anybody know what I am doing wrong? 有人知道我在做什么错吗?

------------------------ Update ------------------------ ------------------------ 更新 ------------------------

Off the back of this answer , I did not have @EnableWebMvc on my MvcConfiguration class. 关闭这个后面的答案 ,我没有@EnableWebMvc我MvcConfiguration类。 I added it in, and the view was resolved and everything started working. 我添加了它,视图已解决,一切开始工作。

Spring Boot will automatically add static web resources located within any of the following directories: Spring Boot将自动添加位于以下任何目录中的静态Web资源:

  • /META-INF/resources/ / META-INF /资源/
  • /resources/ /资源/
  • /static/ /静态的/
  • /public/ /上市/

You need to create some of the directories that spring wants, create a src/main/resources/index.html 您需要创建一些spring需要的目录,创建一个src/main/resources/index.html

Regards, 问候,

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring 微服务:servlet [jsp] 的 Servlet.service() 抛出异常 - Spring microservice: Servlet.service() for servlet [jsp] threw exception servlet [jsp] 的 Servlet.service() 抛出异常 - Servlet.service() for servlet [jsp] threw exception servlet [dispatcherServlet] 在路径 [] 的上下文中的 Servlet.service() 引发异常 [处理程序调度失败; - Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Handler dispatch failed; servlet [dispatcher] 的 Servlet.service() 在路径为 [/***] 的上下文中抛出异常 - Servlet.service() for servlet [dispatcher] in context with path [/***] threw exception JBWEB000236:servlet strutsAction 的 Servlet.service() 抛出异常:java.lang.StackOverflowError - JBWEB000236: Servlet.service() for servlet strutsAction threw exception: java.lang.StackOverflowError 严重:Servlet [jsp]的Servlet.service()抛出异常java.lang.NumberFormatException - SEVERE: Servlet.service() for servlet [jsp] threw exception java.lang.NumberFormatException Servlet jsp的Servlet.service()引发了异常java.lang.IllegalStateException - Servlet.service() for servlet jsp threw exception java.lang.IllegalStateException servlet的标准包装器值[register]:servlet.service()抛出异常java.lang.nullpointerexception; - standard wrapper value[register]:servlet.service() for servlet threw exception java.lang.nullpointerexception; 为什么servlet jsp的Servlet.service()抛出异常java.lang.NullPointerException? - Why Servlet.service() for servlet jsp threw exception java.lang.NullPointerException? Servlet默认的Servlet.service()抛出异常java.lang.NullPointerException - Servlet.service() for servlet default threw exception java.lang.NullPointerException
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM