0

所以这对我来说真的很奇怪。我有我正在编写的这个 spring 应用程序,当我启动服务器并http://localhost/ims/resources/index.html第一次导航到它时,它给了我以下错误:

SEVERE: Servlet.service() for servlet [appServlet] in context with path [/ims] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause
java.lang.NullPointerException
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:848)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:827)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:722)

但是,如果我刷新页面,我没有任何问题。它只发生在第一个请求上。这是我的资源配置的样子:

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.mypackage.ims.web")
public class IMSAppConfig extends WebMvcConfigurerAdapter
{
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry)
    {
        registry.addResourceHandler("/resources/**").addResourceLocations("ims/resources/");
    }

    ....
}

任何想法为什么会发生这种情况?

4

1 回答 1

0

您需要在 context.xml 文件中提供基本包扫描详细信息和其他详细信息。以便在部署项目后加载配置。

但是在你的情况下,搜索映射 url 的包只有在执行时才知道 spring,所以只有在程序第一次执行之后 spring 映射 url 并且当你刷新时(第二次点击服务器time) 找到了 url。

于 2013-11-09T08:25:01.197 回答