Deploying WAR in tomcat

I installed tomcat on archlinux, I tried both tomcat7 and tomcat8. According to several sources, including official documentation, deploying a WAR file is as simple as deleting it in the webapps folder (which in my case is / var / lib / tomcat7 / webapps). WAR file exploded. However, I cannot figure out how to access my web application. On localhost: 8080 there is a tomcat webpage. I also tried localhost: 8080 / name-of-the-war-file, but this is only allowed for 404 HTTP status.

The application I used for testing is the first spring download guide: http://spring.io/guides/gs/spring-boot/

I modified the maven pom.xml build file to create a WAR file when I run the 'mvn package':

<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.springframework</groupId> <artifactId>gs-spring-boot</artifactId> <version>0.1.0</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.2.2.RELEASE</version> </parent> <packaging>war</packaging> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> </dependencies> <properties> <java.version>1.7</java.version> </properties> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project> 
0
source share
1 answer

In addition to modifying pom.xml to create a WAR file, you also need to configure tomcat for the web application. Previously, this was done through the web.xml file. Currently, this can be archived from the application itself. SpringBootServletInitializer and WebApplicationInitializer are search hints. The easiest way I worked with was to modify Application.java as follows:

 package hello; import java.util.Arrays; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ApplicationContext; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.context.web.SpringBootServletInitializer; @SpringBootApplication public class Application extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(Application.class); } public static void main(String[] args) { ApplicationContext ctx = SpringApplication.run(Application.class, args); System.out.println("Let inspect the beans provided by Spring Boot:"); String[] beanNames = ctx.getBeanDefinitionNames(); Arrays.sort(beanNames); for (String beanName : beanNames) { System.out.println(beanName); } } } 

Now I see "Hello from Spring Boot!" to http: // localhost: 8080 / gs-spring-boot-0.1.0 / (with gs-spring-boot -0.1.0.war is the name of the deployed WAR file).

0
source

Source: https://habr.com/ru/post/986882/


All Articles