SpringBoot应用程序打包失败,但可以在IDE中正常运行

时间:2019-04-04 08:35:54

标签: java spring-boot

我认为该错误的原因与MyWebMvcConfigurerAdapter类有关,该类可以通过将其删除而成功打包。

通过删除MyWebMvcConfigurerAdapter类可以成功打包程序

@Configuration
public class MyWebMvcConfigurerAdapter implements WebMvcConfigurer {
    /**
     * 
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        ApplicationHome home = new ApplicationHome(getClass());
        File jarFile = home.getSource();
        registry.addResourceHandler("/static/**")
                .addResourceLocations("classpath:/META-INF/resources/")
                .addResourceLocations("classpath:/resources/")
                .addResourceLocations("classpath:/static/")
                .addResourceLocations("classpath:/public/")
                .addResourceLocations("classpath:/META-INF/resources/WEB-INF/static/")
            .addResourceLocations("file:"+jarFile.getParentFile().toString()+"\\static\\");
    }
}





Caused by: java.lang.NullPointerException: null
    at com.hzh.menu.follow_your_heart.client.config.webconfig.MyWebMvcConfigurerAdapter.addResourceHandlers(MyWebMvcConfigurerAdapter.java:27)
    at org.springframework.web.servlet.config.annotation.WebMvcConfigurerComposite.addResourceHandlers(WebMvcConfigurerComposite.java:95)
    at org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration.addResourceHandlers(DelegatingWebMvcConfiguration.java:88)
    at org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport.resourceHandlerMapping(WebMvcConfigurationSupport.java:490)
    at org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration$EnableWebMvcConfiguration$$EnhancerBySpringCGLIB$$ef76c66.CGLIB$resourceHandlerMapping$38(<generated>)
    at org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration$EnableWebMvcConfiguration$$EnhancerBySpringCGLIB$$ef76c66$$FastClassBySpringCGLIB$$a1243485.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244)
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:363)
    at org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration$EnableWebMvcConfiguration$$EnhancerBySpringCGLIB$$ef76c66.resourceHandlerMapping(<generated>)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154)
    ... 46 common frames omitted
[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 12.824 s <<< FAILURE! - in com.hzh.menu.follow_your_heart.FollowYourHeartApplicationTests
[ERROR] contextLoads(com.hzh.menu.follow_your_heart.FollowYourHeartApplicationTests)  Time elapsed: 0.018 s  <<< ERROR!
java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'resourceHandlerMapping' defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.servlet.HandlerMapping]: Factory method 'resourceHandlerMapping' threw exception; nested exception is java.lang.NullPointerException
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.servlet.HandlerMapping]: Factory method 'resourceHandlerMapping' threw exception; nested exception is java.lang.NullPointerException
Caused by: java.lang.NullPointerException

我希望程序可以成功打包

1 个答案:

答案 0 :(得分:0)

通过查阅API文档,我知道我应该使用ApplicationHome。 getDir(),而不是ApplicationHome。 getSource()。在单元测试环境中,getSource()将返回null。

Method Detail
getSource
public File getSource()
Returns the underlying source used to find the home directory. This is usually the jar file or a directory. Can return null if the source cannot be determined.
Returns:the underlying source or null

getDir
public File getDir()
Returns the application home directory.
Returns:the home directory (never null)