skip to Main Content

I can’t access the actuator or swagger doc endpoints when I compile my app and run the jar from my target folder. But, I am able to access other endpoints with my app.

I can access these endpoints when I run my app directly from Intellj.

I think there must be some sort of problem with how I’m packaging my project in maven. I’ve included the pom.xml below.

application.properties

# Server
server.port = 9000
server.servlet.context-path = /api

# App
app.service_name = my-api
app.batch_limit = 100

# Health
management.endpoints.web.exposure.include = health,info
management.endpoint.health.show-details = ALWAYS
management.endpoints.web.base-path = /
management.endpoints.web.path-mapping.health = /_health
management.health.neo4j.enabled = false

DockerFile

FROM openjdk:17-alpine

RUN apk --no-cache add curl

WORKDIR /app

COPY ./my-api.jar /app/
COPY ./application.properties /app/config

EXPOSE 9000

CMD sleep 30 ; exec java $JAVA_OPTS -jar ./my-api.jar

pom.xml


<dependencies>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>${spring.version}</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <version>${spring-boot.version}</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-webflux</artifactId>
    <version>${spring-boot.version}</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-validation</artifactId>
    <version>${spring-boot.version}</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
    <version>${spring-boot.version}</version>
</dependency>
<dependency>
    <groupId>io.swagger.core.v3</groupId>
    <artifactId>swagger-annotations</artifactId>
    <version>${swagger.version}</version>
</dependency>
<dependency>
    <groupId>org.springdoc</groupId>
    <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
    <version>${spring-doc.version}</version>
    <exclusions>
        <exclusion>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
        </exclusion>
    </exclusions>
</dependency>
</dependencies>

<build>
<plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>${maven-compiler.version}</version>
        <configuration>
            <release>17</release>
        </configuration>
    </plugin>

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>${maven-jar.version}</version>
        <configuration>
            <archive>
                <manifest>
                    <addClasspath>true</addClasspath>
                    <classpathPrefix>src</classpathPrefix>
                    <mainClass>com.api.myApp</mainClass>
                </manifest>
            </archive>
        </configuration>
    </plugin>

    <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>${maven-assembly.version}</version>
        <executions>
            <execution>
                <phase>package</phase>
                <goals>
                    <goal>single</goal>
                </goals>
            </execution>
        </executions>
        <configuration>
            <archive>
                <manifest>
                    <addClasspath>true</addClasspath>
                    <classpathPrefix>src</classpathPrefix>
                    <mainClass>com.api.myApp</mainClass>
                </manifest>
            </archive>
            <descriptorRefs>
                <descriptorRef>jar-with-dependencies</descriptorRef>
            </descriptorRefs>
        </configuration>
    </plugin>
</plugins>
</build>

2

Answers


  1. Chosen as BEST ANSWER

    My project seems to build correctly when using the spring-boot-maven-plugin instead of maven-assembly-plugin. I can now access health and swagger endpoints.

    <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <version>${spring-boot-maven.version}</version>
        <configuration>
            <mainClass>com.api.myApp</mainClass>
        </configuration>
        <executions>
            <execution>
                <goals>
                    <goal>repackage</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
    

  2. From what you’ve posted, I assume that you deploy your application to a local docker container. You specify the health endpoint to be on port 9000 of the container. Which is not, by default, the same as port 9000 of your machine (a.k.a. localhost).

    From the dockerfile reference documentation:

    The EXPOSE instruction informs Docker that the container listens on the specified network ports at runtime. You can specify whether the port listens on TCP or UDP, and the default is TCP if the protocol is not specified.

    The EXPOSE instruction does not actually publish the port. It functions as a type of documentation between the person who builds the image and the person who runs the container, about which ports are intended to be published. To actually publish the port when running the container, use the -p flag on docker run to publish and map one or more ports, or the -P flag to publish all exposed ports and map them to high-order ports.

    (You could, in theory, run multiple containers on the same machine that all specify internally to listen to port 9000. On container startup, you would then map those container ports to different ports of your machine.)

    When you run the application in Intellij, port 9000 there actually means port 9000 of your machine.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search