简体   繁体   English

无法在首次请求时加载资源

[英]Can't load resource on first request

So this is really weird to me. 所以这对我来说真的很奇怪。 I have this spring application I am writing and when I start the server and navigate to http://localhost/ims/resources/index.html for the first time it gives me the following error: 我有我正在编写的这个春季应用程序,当我启动服务器并第一次导航到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)

However if I refresh the page I don't have any problems. 但是,如果我刷新页面,则不会有任何问题。 It only happens on the very first request. 它仅在第一次请求时发生。 This is what my resource config looks like: 这是我的资源配置:

@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/");
    }

    ....
}

Any ideas why this might be happening? 任何想法为什么会发生这种情况?

You need to give the base package scanning details and other details in context.xml file. 您需要在context.xml文件中为基本软件包提供扫描详细信息和其他详细信息。 So that the configuration will be loaded once the project is deployed. 这样,在部署项目后便会加载配置。

But in your case the package to search for the mapping urls is known to the spring only at the exectution time, So only after the program executes for the first time the spring maps down the urls and when you refresh (hit the server for the 2nd time) the url is found. 但是在您的情况下,弹簧只在执行时才知道用于搜索映射URL的包,因此,只有在程序第一次执行后,弹簧才会向下映射URL并在您刷新时(第二次击中服务器)时间)找到网址。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM