Imtiaz Mirza
Imtiaz Mirza

Reputation: 591

Spring-boot jersey maven failed to run war file

We are creating a spring-boot jersey application. Now we want to create executable war file. The problem is the application runs fine when I run it with

  mvn spring-boot:run 

But when I try to package it to war and run it with java -jar ABD.war its giving the following error

 Caused by: java.io.FileNotFoundException:       /Users/ABC/ABD-0.0.1-SNAPSHOT.war!/WEB-INF/classes (No such file or directory)


 Caused by: org.glassfish.jersey.server.internal.scanning.ResourceFinderException:

Here are the part of pom.xml I'm using ,

<packaging>war</packaging>
.
.
.

 <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>


        <org.slf4j.version>1.7.7</org.slf4j.version>

        <maven-compiler-plugin.version>3.1</maven-compiler-plugin.version>

        <java.version>1.8</java.version>


  </properties>

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

Although when I unpack the war file I can see the WEB-INF/classes folder is there.

Upvotes: 3

Views: 2663

Answers (1)

Imtiaz Mirza
Imtiaz Mirza

Reputation: 591

OK found the solution. I have a jersery config class, where I added all of controllers class with packages(). When I commented it out and change it to register("controller.class") It started to work!

    @Configuration
    @ApplicationPath("/path")
    @Controller 
    public class JerseyConfig extends ResourceConfig {
        public JerseyConfig() {
            register(MultiPartFeature.class);
           register(OneController.class);
          //packages("com.controllers");
        }
    }
#

Update

#
private static final Logger logger = LoggerFactory.getLogger(OneController.class);

  public JerseyConfig() {
     scan("com.somepackages");
  }

  public void scan(String... packages) {
      for (String pack : packages) {
          Reflections reflections = new Reflections(pack);
           reflections.getTypesAnnotatedWith(Path.class)
           .parallelStream()
           .forEach((clazz) -> {
              logger.info("New resource registered: " + clazz.getName());
           register(clazz);
      });
 }

}

#

With this solution you can get all controllers in jersey register through package scan.

Upvotes: 10

Related Questions