官术网_书友最值得收藏!

  • Spring 5.0 Projects
  • Nilang Patel
  • 303字
  • 2021-07-02 12:34:58

Configuration to deploy to Tomcat without web.xml

As we will be deploying the application to Tomcat, we need to provide the servlet configuration to the application server. We will look at how to deploy to Tomcat in a separate section, but now we will look at the Java configuration, which is sufficient to deploy the application to Tomcat or any application server without the need for an additional web.xml. The Java class definition is given in the following:

public class WorldApplicationInitializer extends
AbstractAnnotationConfigDispatcherServletInitializer {

@Override
protected Class<?>[] getRootConfigClasses() {
return null;
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[] {AppConfiguration.class};
}
@Override
protected String[] getServletMappings() {
return new String[] { "/" };
}
}

The AbstractAnnotationConfigDispatcherServletInitializer abstract class is an implementation of the WebApplicationInitializer interface that is used to register Spring's DispatcherServlet instance and uses the other @Configuration classes to configure the DispatcherServlet.

We just need to override the getRootConfigClasses(), getServletConfigClasses(), and getServletMappings() methods. The first two methods point to the configuration classes that need to load into the servlet context, and the last method is used to provide the servlet mapping for DispatcherServlet.

DispatcherServlet follows the front controller pattern, where there is a single servlet registered to handle all the web requests. This servlet uses the RequestHandlerMapping and invokes the corresponding implementation based on the URL mapped to the implementation.

We need to make a small update to the Maven WAR plugin so that it doesn't fail if there is no web.xml found. This can be done by updating the <plugins> tag in the pom.xml file, as shown in the following:

<build>
<finalName>worldgdp</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<executions>
<execution>
<id>default-war</id>
<phase>prepare-package</phase>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

Now we are all set to implement our controllers. We will show you how to deploy to Tomcat once we have implemented all the RESTful API controllers.

主站蜘蛛池模板: 无棣县| 菏泽市| 黄平县| 松桃| 罗山县| 台北县| 周口市| 鄂托克旗| 临桂县| 平和县| 卓尼县| 古蔺县| 宜兰县| 略阳县| 曲松县| 安乡县| 乌鲁木齐县| 枞阳县| 河西区| 玉门市| 安多县| 米易县| 黄冈市| 龙井市| 吴旗县| 汾阳市| 开封县| 吉隆县| 岢岚县| 泸溪县| 襄垣县| 沙雅县| 寿阳县| 梅河口市| 丰台区| 沅江市| 龙州县| 得荣县| 石林| 尉氏县| 朝阳市|