Deploy Spring Boot to Tomcat

后端 未结 3 1276
北荒
北荒 2020-12-02 11:06

So I am new to Spring - so I thought I would try Spring Boot

I am using a Maven to build - I set up a view requests, when I run it as \"App\" looks like it starts t

相关标签:
3条回答
  • 2020-12-02 11:12

    Below code worked fine for tomcat8 deployment without tomcat dependency.

    @SpringBootApplication
    @EnableAutoConfiguration
    @ComponentScan
    public class ManufacturingRegionApplication extends SpringBootServletInitializer {
    
    public static void main(String[] args) {
        new SpringApplicationBuilder(ManufacturingRegionApplication.class).application().run(args);
    }
    
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder applicationBuilder) {
        return applicationBuilder.sources(application);
    }
    
    private static Class<ManufacturingRegionApplication> application = ManufacturingRegionApplication.class;
    

    }

    Below dependency is not required.

       <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
    

     @RestController
     @RequestMapping(value = "/manufacturing-region-service")
     public class ManufacturingRegionService {
    
    @Resource
    private ManufacturingRegionDao manufacturingRegionDao;
    
    @ResponseBody
    @Transactional(readOnly = true)
    @RequestMapping(value = "/region-codes/{abbr}", method = GET, produces = "application/json")
    

    http://localhost:8080/manufacturing-region-api/manufacturing-region-service/region-codes/ABBRVALUE

    0 讨论(0)
  • 2020-12-02 11:31

    The chapter Packaging executable jar and war files in the Spring Boot reference documentation states:

    To build a war file that is both executable and deployable into an external container you need to mark the embedded container dependencies as “provided”, e.g:

    <?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">
        <!-- ... -->
        <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>
    </project>
    
    0 讨论(0)
  • 2020-12-02 11:33

    When we choose Spring Boot, we don't want to produce a WAR. From the root page:

    Spring Boot makes it easy to create stand-alone [...] 
    Applications that can you can "just run".
    Embed Tomcat or Jetty directly (no need to deploy WAR files)
    

    EDITED: So, if the purpose is "to try Spring Boot", I suggest to not generate a WAR file.

    If you really need to produce a WAR file (keep your code built against SpringBoot and produce a file that you can run in any standard servlet container...) then you should read the documentation Converting a Spring Boot JAR Application to a WAR.

    Did you include spring-boot-maven-plugin in the build process? You didn't describe this step...

    0 讨论(0)
提交回复
热议问题