1
votes

I'm trying to debug a problem with deploying an existing spring-boot app as a .war file and can't make it work in even a minimal case.

Specifically I:

The resulting war file appears to deploy within an instance of Jetty (9.4.11.v20180605) with the following context...

<?xml version="1.0"  encoding="ISO-8859-1"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure.dtd">

<Configure class="org.eclipse.jetty.webapp.WebAppContext">
    <Set name="contextPath">/some-context-path</Set>
    <Set name="war">/some-path/gs-spring-boot-0.1.0.war</Set>
</Configure>

However accessing the application in a browser provides a directory listing of META_INF, WEB-INF and org rather than the expected 'Greetings from Spring Boot!'.

I'm assuming, perhaps unfairly, that my problem isn't the jetty setup because it's working fine for a bunch of other .war files, but perhaps I'm missing something I need to set in Jetty to have it work with the type of .war files spring-boot is producing.

So, to sum up the question, what required step am I missing to produce a spring-boot .war file I can deploy in an existing container?


The three files I ended up with in the project after following the steps above are shown below for reference in case it helps.

pom.xml

<?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>

    <packaging>war</packaging>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.5.RELEASE</version>
    </parent>

    <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.8</java.version>
    </properties>


    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

src/main/java/hello/HelloController.java

package hello;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;

@RestController
public class HelloController {

    @RequestMapping("/")
    public String index() {
        return "Greetings from Spring Boot!";
    }

}

src/main/java/hello/Application.java

package hello;

import java.util.Arrays;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class Application extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }

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

    @Bean
    public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
        return args -> {

            System.out.println("Let's inspect the beans provided by Spring Boot:");

            String[] beanNames = ctx.getBeanDefinitionNames();
            Arrays.sort(beanNames);
            for (String beanName : beanNames) {
                System.out.println(beanName);
            }

        };
    }

}
1
Ah, downloading the latest jetty distribution (9.4.12), copying the war file into the top level webapps then running java -jar start.jar seems to work fine, so it seems that whatever the problem is, it's a problem with how I have my jetty container configured. - Matt Sheppard

1 Answers

0
votes

I think my problem was that the embedded jetty I was using was not including the org.eclipse.jetty.annotations.AnnotationConfiguration processor.

Adding some code based on https://www.eclipse.org/jetty/documentation/9.4.x/using-annotations-embedded.html to the setup of the jetty server object seems like it got it working at least in this minimal case.