1

I have a spring boot (2.5.3) app running on a centOS VM behind a firewall. I normally build a fat jar, then run it with a config passed via CLI:

  1. mvn clean package spring-boot:repackage
  2. java -jar target/service.jar --spring.config.location=/path/to/config.properties
  3. run curl GET commands: curl --key /a/b --cert /x/y "https://server-name:8767/path?arg=..."

It works using port 8767 set in the config, and I chose this port a while back randomly.

Since then, I've tried to see if I could make it work with a different port. I opened more ports on the linux public firewall-cmd zone, including 8768 & 9000. Problem is that no matter what I try, the only port I can get the app to run on is 8767. Seems like I've somehow hard-wired it to that port!

Normally server.port is set in the config, but even if I pass another port --server.port=xxxx via cli, the app runs, and logs show it is exposed to xxxx; however, curl can consistently only access 8767, and other ports time out. Or if I set server.port=xxxx in the config, same outcome.

What do I need to do to use a different port? (I saw this...would it help me?)

Dependencies (nothing special) Dependencies (nothing special)

<parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
</parent>
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
  <groupId>org.apache.commons</groupId>
  <artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
  <groupId>org.projectlombok</groupId>
  <artifactId>lombok</artifactId>
</dependency>
<dependency>
  <groupId>org.apache.httpcomponents</groupId>
  <artifactId>httpclient</artifactId>
</dependency>

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
</dependency>

UPDATE: @Vinit - my main class is exactly like yours, except a std println I have to let me know it's running:

System.out.println("Running...");

As for my application.properties, I cannot paste them as I'm behind a firewall, but they are basically below, and there are more than one of each:

logging.level
server.port=xxxx  // as described above, i've tried declaring here or cli
server.ssl
# custom auth properties
customauth.url
spring.profiles.active
spring.application.name
spring.task.scheduling
spring.jmx.enabled
swagger
management.endpoints
sanitization
spring.jackson

On another note, I run

sudo netstat -nlp | grep "<port>"

...before I run the app (where is the port I have either in my config or passed CLI), and no results. Then I run the app, repeat the netstat call, and that port is listening sure enough. But same thing: if 8767, all is well; but if 8768, time out.

6
  • 1. can you just check your Main class, how you are executing SpringApplication? 2. can you check if there is existing application running on port 8767 Commented Jul 29, 2022 at 17:25
  • You are using ssl, are you sure there is no web server on that port like httpd or nginx in front of your java app? Commented Jul 29, 2022 at 18:36
  • @slindenau pretty sure. I'll check and get back to you when I'm able to check in a couple of days unfortunately. Might have to repost this question then Commented Jul 29, 2022 at 21:20
  • @kus I have a basic spring boot main class annotated with SpringBootApplication. Nothing fancy. So if I can only get my app to run on 8767, why would there also be something else running on that? Commented Jul 29, 2022 at 21:22
  • @kus If there were something else on 8767, then my app would not run Commented Jul 29, 2022 at 21:33

2 Answers 2

1

Spring boot takes into account cli arguments when you pass the arguments to SpringApplication.run method in the main method. Main class should look like this -

@SpringBootApplication
public class Application {

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

Pass args as argument to run method and it should take cli arguments into account. With this class, if --server.port=8080 is used as cli argument, then spring application should run on 8080 port.

Sign up to request clarification or add additional context in comments.

3 Comments

that is the standard, i know, but my issue remains
Can you please share your class and config.properties.
Yes @Vinit see UPDATE above
0

this is the order of how spring boots evaluates the different approaches of setting the server port are:

  1. embedded server configuration
  2. command-line arguments
  3. property files
  4. main @SpringBootApplication configuration

so two typical issues if the server.port parameter does not work are overridden behavior in a WebServerFactoryCustomizer or in your main method of the SpringBootApplication

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.