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
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.
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:
(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.