How to Expose a REST API on a Different Port in Spring Boot Without Causing a PortInUseException?

I’m trying to expose a separate REST API in my Spring Boot application, bound to a different port from the default server and management ports. To achieve this, I created a new TomcatServletWebServerFactory and bound the new port to the container. I also set up a new GenericWebApplicationContext and linked my REST controller to this context.

Additionally, I transferred the default parent context to the new context, so that the beans and configurations are shared between them.

However, when I attempt to launch the new child context, I encounter a PortInUseException. Despite this error, the application itself continues running, but the new port doesn’t open as expected.

Here’s an outline of my approach:

  • Created a new TomcatServletWebServerFactory.
  • Set a custom port for the new container.
  • Created a new GenericWebApplicationContext.
  • Transferred the parent context for bean access.
  • Connected the REST controller to the new context.

Issue: The new context throws a PortInUseException, and the separate port fails to start, even though the main service continues functioning normally. How can I resolve this issue and get the new port working?

Any guidance or insights would be greatly appreciated. Thank you!

Tomcat Configuration Class:

@Slf4j
@Configuration
public class TopologyServerConfig {

    private final ApplicationContext defaultCtx;

    @Value("${server.topology.port:8099}")
    private int topologyServerPort;

    @Value("${spring.lifecycle.timeout-per-shutdown-phase:3000}")
    private long timeOutPerShutdownPhase;

    public TopologyServerConfig(ApplicationContext defaultCtx) {
        this.defaultCtx = defaultCtx;
    }

    @Bean
    public ServletWebServerFactory topologyServletContainer() {
        TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
        factory.setPort(topologyServerPort);  // Set port to 8099
        return factory;
    }

    @EventListener(ApplicationReadyEvent.class)
    public void startTopologyCtxAfterDefaultCtx() {
        log.info("Default context is fully started. Now starting topology context on port {}...", topologyServerPort);

        // Create a new context for the topology server
        GenericWebApplicationContext topologyCtx = new GenericWebApplicationContext();
        topologyCtx.setParent(defaultCtx);
        topologyCtx.registerBean(StreamTopologyDisplayController.class);
        topologyCtx.registerBean(ServletWebServerFactory.class, this::topologyServletContainer);
        topologyCtx.registerShutdownHook();

        try {
            ServletWebServerFactory topologyFactory = topologyServletContainer();
            WebServer topologyServer = topologyFactory.getWebServer(servletContext -> {
                topologyCtx.setServletContext(servletContext);
                topologyCtx.refresh();
            });

            topologyServer.start(); // <- exception happening form this line
            log.info("Topology web server started at port {}", topologyFactory.getWebServer().getPort());
        } catch (Exception e) {
            log.error("Failed to start topology web server on port " + topologyServerPort, e);
            System.exit(1);
        }
    }
}

REST Controller class:

@Slf4j
@RestController
@RequestMapping("/topology")
public class StreamTopologyDisplayController {

    @GetMapping
    public String getStreamTopology() {
        log.info("hit the getStreamTopology");
        if (Constant.STREAM_TOPOLOGY_DESCRIPTION.isEmpty()) {
            return "Not Found";
        }

        return Constant.STREAM_TOPOLOGY_DESCRIPTION;
    }
}

Exception:

org.springframework.boot.web.server.PortInUseException: Port 8097 is already in use

Issue: The new context throws a PortInUseException, and the separate port fails to start, even though the main service continues functioning normally. How can I resolve this issue and get the new port working?

Any guidance or insights would be greatly appreciated. Thank you!

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật