Spring Boot war在外部Tomcat中引发异常。

huangapple go评论82阅读模式
英文:

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 :

&lt;packaging&gt;war&lt;/packaging&gt;

&lt;build&gt;
		&lt;plugins&gt;
			&lt;plugin&gt;
				&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
				&lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
			&lt;/plugin&gt;
		&lt;/plugins&gt;
	&lt;/build&gt;

Also have a dependency like this,

&lt;dependency&gt;
			&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
			&lt;artifactId&gt;spring-boot-starter-tomcat&lt;/artifactId&gt;
			&lt;scope&gt;provided&lt;/scope&gt;
		&lt;/dependency&gt;

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);

    }
}

huangapple
  • 本文由 发表于 2020年10月12日 16:24:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/64314165.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定