英文:
Spring Boot war giving exception in external tomcat
问题
我在STS IDE中开发了一个Spring Boot应用程序,并且能够在没有任何异常的情况下使用主类运行。
@SpringBootApplication
public class Java3Application {
public static void main(String[] args) {
SpringApplication.run(Java3Application.class, args);
}
}
我使用以下插件和打包生成了相同项目的war包:
<packaging>war</packaging>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
还有一个类似这样的依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
但是生成的war包在命中我的某个REST控制器时抛出空指针异常,但是使用主类时却正常工作。在我的情况下发生了什么?我的war打包有问题吗?或者不够完整?
英文:
I have developed a spring boot application in STS ide and i was able to run with the main class without any exception.
@SpringBootApplication
public class Java3Application {
public static void main(String[] args) {
SpringApplication.run(Java3Application.class, args);
}
}
public class ServletInitializer extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
setRegisterErrorPageFilter(false);
return application.sources(Java3Application.class);
}
}
I generated the war of the same project using the below plugin and packaging :
<packaging>war</packaging>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
Also have a dependency like this,
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
But the generated war is giving null pointer exception when one of my rest controllers are getting hit, but same is working fine with Main class.
What is happening in my case?? Is my war packaging is wrong or in sufficient??
答案1
得分: 1
为了使一个Spring Boot应用在外部Tomcat中运行,您需要将其扩展为SpringBootServletInitializer。请将您的代码修改如下:
@SpringBootApplication
public class Java3Application extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(Java3Application.class, args);
}
}
英文:
To make a spring boot application run in external tomcat, you have to extend it to SpringBootServletInitializer. Modify your code as below
@SpringBootApplication
public class Java3Application extends SpringBootServletInitializer{
public static void main(String[] args) {
SpringApplication.run(Java3Application.class, args);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论