springboot+jsp项目打包成jar/war包404解决方法

springboot+jsp项目打包成jar/war包404解决方法

描述:springboot项目,前端使用jsp,现在需要将写有jsp页面的服务部署到服务器上,打包出现问题,导致部署不上去

环境参数
开发环境:inteliJ IDEA 2018
JDK:Java 1.8


过程

打包成war包

我项目里的pom.xml开始配置为war包,打包成war部署需要
1.如果配置为jar要改成war

2.在pom.xml中添加一个dependency依赖(tomcat是外部提供的)


   org.springframework.boot
   spring-boot-starter-tomcat
   provided
 

3.创建web.xml有两种方式,一种是自己创建文件夹和文件,另一种创建文件夹,IDEA创建文件
springboot+jsp项目打包成jar/war包404解决方法_第1张图片
4.在启动类添加(也可以创建一个Initializer类),继承自SpringBootServletInitializer,并重写configure方法,为了打包springboot项目用的。
springboot+jsp项目打包成jar/war包404解决方法_第2张图片
3.打包方式可以用命令
$ mvn clean package
或者使用idea的插件打包,然后target目录下会出现war包

使用war包放到tomcat的webapp下,启动时会解压

项目启动

我项目最开始放jsp的web-site服务的启动类是启动不了的,找到另一种启动方法使用springboot的插件来启动是可以的,就一直使用插件启动,插件启动是:
springboot+jsp项目打包成jar/war包404解决方法_第3张图片
启动类无法使用,就无法打包成jar包启动(jar包启动默认是先去访问启动类)


使用war包报错

但是当我打包成war包就一直报错:

09-Aug-2019 10:15:00.896 严重 [main] org.apache.catalina.startup.HostConfig.deployWAR 部署 Web 应用程序 archive [E:\apache-tomcat-9.0.21\webapps\index.war] 时出错
java.lang.IllegalStateException: Error starting child
		at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:720)
		at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:690)
		at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:705)
		at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:978)
		at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1849)
		at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
		at java.util.concurrent.FutureTask.run(FutureTask.java:266)
		at org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75)
		at java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:112)
		at org.apache.catalina.startup.HostConfig.deployWARs(HostConfig.java:773)
		at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:427)
		at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1576)
		at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:309)
		at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:123)
		at org.apache.catalina.util.LifecycleBase.setStateInternal(LifecycleBase.java:423)
		at org.apache.catalina.util.LifecycleBase.setState(LifecycleBase.java:366)
		at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:936)
		at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:841)
		at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
		at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1384)
		at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1374)
		at java.util.concurrent.FutureTask.run(FutureTask.java:266)
		at org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75)
		at java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:134)
		at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:909)
		at org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:262)
		at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
		at org.apache.catalina.core.StandardService.startInternal(StandardService.java:421)
		at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
		at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:932)
		at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
		at org.apache.catalina.startup.Catalina.start(Catalina.java:633)
		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.apache.catalina.startup.Bootstrap.start(Bootstrap.java:344)
		at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:475)
	Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/index]]
		at org.apache.catalina.util.LifecycleBase.handleSubClassException(LifecycleBase.java:440)
		at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:198)
		at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:717)
		... 37 more
	Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'webSiteController': Unsatisfied dependency expressed through field 'queryIndexPurchaserListClient'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.epc.web.site.remoteApi.QueryIndexPurchaserListClient': FactoryBean threw exception on object creation; nested exception is java.lang.TypeNotPresentException: Type com.epc.web.facade.contend.cpurchaser.vo.index.QueryIndexPurchaserVO not present
		at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:588)
		at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
		at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:366)
		at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1264)
		at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)
		at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
		at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
		at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
		at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
		at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
		at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761)
		at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867)
		at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543)
		at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122)
		at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693)
		at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360)
		at org.springframework.boot.SpringApplication.run(SpringApplication.java:303)
		at org.springframework.boot.web.support.SpringBootServletInitializer.run(SpringBootServletInitializer.java:151)
		at org.springframework.boot.web.support.SpringBootServletInitializer.createRootApplicationContext(SpringBootServletInitializer.java:131)
		at org.springframework.boot.web.support.SpringBootServletInitializer.onStartup(SpringBootServletInitializer.java:86)
		at org.springframework.web.SpringServletContainerInitializer.onStartup(SpringServletContainerInitializer.java:169)
		at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5132)
		at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
		... 38 more
	Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.epc.web.site.remoteApi.QueryIndexPurchaserListClient': FactoryBean threw exception on object creation; nested exception is java.lang.TypeNotPresentException: Type com.epc.web.facade.contend.cpurchaser.vo.index.QueryIndexPurchaserVO not present
		at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:175)
		at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.getObjectFromFactoryBean(FactoryBeanRegistrySupport.java:103)
		at org.springframework.beans.factory.support.AbstractBeanFactory.getObjectForBeanInstance(AbstractBeanFactory.java:1634)
		at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:254)
		at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
		at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:208)
		at org.springframework.beans.factory.support.DefaultListableBeanFactory.addCandidateEntry(DefaultListableBeanFactory.java:1316)
		at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1282)
		at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1101)
		at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066)
		at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:585)
		... 60 more
	Caused by: java.lang.TypeNotPresentException: Type com.epc.web.facade.contend.cpurchaser.vo.index.QueryIndexPurchaserVO not present
		at sun.reflect.generics.factory.CoreReflectionFactory.makeNamedType(CoreReflectionFactory.java:117)
		at sun.reflect.generics.visitor.Reifier.visitClassTypeSignature(Reifier.java:125)
		at sun.reflect.generics.tree.ClassTypeSignature.accept(ClassTypeSignature.java:49)
		at sun.reflect.generics.visitor.Reifier.reifyTypeArguments(Reifier.java:68)
		at sun.reflect.generics.visitor.Reifier.visitClassTypeSignature(Reifier.java:138)
		at sun.reflect.generics.tree.ClassTypeSignature.accept(ClassTypeSignature.java:49)
		at sun.reflect.generics.visitor.Reifier.reifyTypeArguments(Reifier.java:68)
		at sun.reflect.generics.visitor.Reifier.visitClassTypeSignature(Reifier.java:138)
		at sun.reflect.generics.tree.ClassTypeSignature.accept(ClassTypeSignature.java:49)
		at sun.reflect.generics.repository.MethodRepository.getReturnType(MethodRepository.java:68)
		at java.lang.reflect.Method.getGenericReturnType(Method.java:255)
		at feign.Contract$BaseContract.parseAndValidateMetadata(Contract.java:85)
		at org.springframework.cloud.netflix.feign.support.SpringMvcContract.parseAndValidateMetadata(SpringMvcContract.java:133)
		at feign.Contract$BaseContract.parseAndValidatateMetadata(Contract.java:64)
		at feign.ReflectiveFeign$ParseHandlersByName.apply(ReflectiveFeign.java:146)
		at feign.ReflectiveFeign.newInstance(ReflectiveFeign.java:53)
		at feign.Feign$Builder.target(Feign.java:218)
		at org.springframework.cloud.netflix.feign.HystrixTargeter.target(HystrixTargeter.java:39)
		at org.springframework.cloud.netflix.feign.FeignClientFactoryBean.loadBalance(FeignClientFactoryBean.java:145)
		at org.springframework.cloud.netflix.feign.FeignClientFactoryBean.getObject(FeignClientFactoryBean.java:166)
		at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:168)
		... 70 more
	Caused by: java.lang.ClassNotFoundException: com.epc.web.facade.contend.cpurchaser.vo.index.QueryIndexPurchaserVO
		at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1365)
		at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1188)
		at java.lang.Class.forName0(Native Method)
		at java.lang.Class.forName(Class.java:348)
		at sun.reflect.generics.factory.CoreReflectionFactory.makeNamedType(CoreReflectionFactory.java:114)
		... 90 more

war包的坑

1.排除spring boot中内嵌的tomcat依赖包:


   org.springframework.boot
   spring-boot-starter-tomcat
   provided

2.打开Tomcat安装后目录,进入conf文件夹,找到配置文件server.xml,将


注释掉。(我使用没能解决问题)

3.查看是否是依赖包有相同的冲突了。

4.添加redis的配置,请检查Redis是否已经启动,或由于防火墙之类的原因无法访问。

5.查看注解是否有问题。
springboot+jsp项目打包成jar/war包404解决方法_第4张图片
两个注解@EnableFeignClients、@EnableDiscoveryClient,启动类少其中任意一个就连接不上webclient(需要调用的另一个服务)

6.查看IDEA里的web.xml生成路径是否有问题。


查找打包成war包需要的配置文件和别人的配置,对比了一天无果,仍然报错,决定使用jar包。

打包成jar包

如果使用jar包就得先把我的启动类修好,所以就先来修复我的启动类了。

打包成jar包需要注意的是:

1.修改pom.xml里的为jar。

2.下面的build这块一定要配置,否则打jar的时候会说找不到主类。


    
        
            org.springframework.boot
            spring-boot-maven-plugin
            
                com.weixin.SmallsystemApplication
            
        
    

3.一种在程序的入口时main函数所在的类,使用==@SpringBootApplication==注解。

另一种和war包一样,在启动类添加(也可以创建一个Initializer类),继承自SpringBootServletInitializer,并重写configure方法。
springboot+jsp项目打包成jar/war包404解决方法_第5张图片
4.在运行面板那有个运行配置,点开,点项目设置working directory工作路径,$MODEULE_DIR(下拉框可选),不知道原理但是配置了我的启动类就可以访问jsp了 。
springboot+jsp项目打包成jar/war包404解决方法_第6张图片
打包成jar包未解决之前报错:
springboot+jsp项目打包成jar/war包404解决方法_第7张图片
5.打包成jar,还是用IDEA工具,先clean再install然后target目录下会出现jar包。
springboot+jsp项目打包成jar/war包404解决方法_第8张图片
以上为springboot项目打包部署我遇到的问题和解决办法,仅供参考,如有问题请大方指出,谢谢。

你可能感兴趣的:(Java相关,前端学习)