SpringBoot app for Tomcat

I have a SpringBoot application that I am trying to deploy to Tomcat Server. According to the online links, I added the code in the Application class as follows:

public class SkyVetApplication extends SpringBootServletInitializer{
...
   @Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
    return application.sources(SkyVetApplication.class);
}
...
}

In build.gradleI added the following:

compile group: 'org.springframework.boot', name: 'spring-boot-starter-web'
**providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'**

After doing a clean build, I copied the war file into the Tomcat folder webapps. But deployment takes place twice and ends with an exception, because the context is already present. What am I missing?

Help is much appreciated.

+4
source share
1 answer

You must add a basic method.

Check out these examples: https://github.com/Pytry/bootiful-war-deployment

Here is an example from the "hello" module (it uses Lombok comment handlers).

package com.example.bootifulwar;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

@SpringBootApplication
@EnableScheduling
@Slf4j
public class HelloServletInitializer extends SpringBootServletInitializer{

    @Value("${messageForUser}")
    private String message;

    @Value("${whatDoesTheFoxSay:'No body knows.'}")
    private String whatDoesTheFoxSay;

    public static void main(String[] args){

        SpringApplication.run(HelloServletInitializer.class, args);
    }

    @Scheduled(fixedRate = 2000)
    public void sayHelloTo(){

        log.info("Hello! " + message);
    }

    @Override
    public SpringApplicationBuilder configure(SpringApplicationBuilder application){

        log.info(
            "\n*********************\n" +
                "What does the fox say?\n" +
                whatDoesTheFoxSay +
                "\n*********************\n");
        return application.sources(HelloServletInitializer.class);
    }
}

"application.properties", , Tomcat, context.xml "conf" /Catalina/ . ".

:

<?xml version='1.0' encoding='utf-8'?>
<Context docBase="hello.war" path="hello">
  <Resources className="org.apache.catalina.webresources.StandardRoot">
    <PreResources base="hello\\config"
                  className="org.apache.catalina.webresources.DirResourceSet"
                  internalPath="/"
                  webAppMount="/WEB-INF/classes"/>
  </Resources>
</Context>

gradlem, .

, .

0

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


All Articles