1

I am currently working on a Springboot application and I need to access the current server port. The ports are being assigned randomly since i have defined server.port = 0 in my application.properties.

I have seen multiple threads about this and they all point to adding:

@Value("${local.server.port}")
private int port;

However booting up the application prompts me with Could not resolve placeholder 'local.server.port' in string value "${local.server.port}"

To clarify, I am not setting up tests. I am using Springboot 1.5.7. Am I missing something? I'm trying to run the following simple example:

@SpringBootApplication
public class Main {

    @Value("${local.server.port}")
    private int port;

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

    @PostConstruct
    public void printsomething() {
        System.out.println("PORT " + this.port);
    }
}
buh
  • 375
  • 5
  • 18
  • of course `${server.port}` works, but returns 0 – buh Oct 22 '17 at 16:09
  • Just asking the obvious, in case...is your application.properties visible (in src/main/resources), i.e. if you remove this port check above and let the app start, is it starting on random port? – Aidan Moriarty Oct 22 '17 at 16:29
  • I am not familiar with spring boot, but I have just read the related section in the document: https://docs.spring.io/spring-boot/docs/current/reference/html/howto-embedded-servlet-containers.html#howto-discover-the-http-port-at-runtime.. you should read the notes also I think. It says "the value is only set once the container has initialized". I think you should follow the document to get the local port from "ApplicationListener" – Surely Oct 22 '17 at 16:32
  • @AidanMoriarty yes, it is in src/main/resources. I can confirm that it is successfully creating random ports. – buh Oct 22 '17 at 16:45
  • @Surely thanks for that, I will look through this doc. But it mentions not to try to inject the port in a regular application (${local.server.port}). At least this clears up why it's failing for me – buh Oct 22 '17 at 16:45
  • you're right @AidanMoriarty, this is duplicated. I have now solved it. Thank you – buh Oct 22 '17 at 18:09

1 Answers1

0

As mentioned in the question comment section and referred documentation that was linked by @Surely, @Value("${local.server.port}") cannot be used when running applications. The solution would be to do the following:

@Component
public class PortListener implements ApplicationListener<EmbeddedServletContainerInitializedEvent> {

    private static int port;

    @Override
    public void onApplicationEvent(final EmbeddedServletContainerInitializedEvent event) {
        port = event.getEmbeddedServletContainer().getPort();
    }

    public static int getPort() {
        return port;
    }
}
buh
  • 375
  • 5
  • 18
  • Repeating the answers from https://stackoverflow.com/a/30312990/5627829 which @surely provided above doesn't really count as you solving it. – Aidan Moriarty Oct 22 '17 at 18:38
  • 2
    I apologize if it came across as so. I have updated the answer accordingly with credit. My intentions was only to leave an answer for any future people stumbling onto this thread. – buh Oct 22 '17 at 20:12