When the SpringBoot project Tomcat deploys the war program, it starts successfully but accesses 404 exception handling

1. Abnormal error

The Springboot project uses IntelliJ IDEA to pack the maven project into a war package, and the pom.xml file introduces dependencies

insert image description here

And after packaging through the maven package, put it under the web-apps file of tomcat

insert image description here

At the same time, the server.xml file under the conf folder was modified, and the following problems occurred when starting tomcat

insert image description here

Two, the reason

SpringApplicationBuilder is used to build Spring applications. It is a builder for SpringApplication and ApplicationContext instances, with convenient and fluent API and context hierarchy support. If SpringApplicationBuilder is not added to the startup class, tomcat will not be able to build SpringBoot applications.

3. Solutions

required in the startup class

  • Inherit the org.springframework.boot.context.web.SpringBootServletInitializer class
  • Override configure(SpringApplicationBuilder application) method

insert image description here

public class CExperimentPlatformApplication extends SpringBootServletInitializer {
    
    

	public static void main(String[] args) {
    
    
		SpringApplication.run(CExperimentPlatformApplication.class, args);
	}

	@Override
	protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
    
    
		return builder.sources(CExperimentPlatformApplication.class);
	}
}

Guess you like

Origin blog.csdn.net/qq_46207024/article/details/130314466