I am just throwing the idea for you to reconsider, it's not production-ready and requires some polishing, but you could definitely try something like this in your main
method:
@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
SpringApplication app = new SpringApplication(MyApplication.class);
int serverPort = 8080;
do {
try {
app.setDefaultProperties( Collections.singletonMap( "server.port", Integer.toString(serverPort) ) );
app.run(args);
} catch (Exception e) {
serverPort++;
}
} while (serverPort < 9000);
}
}
As you might have noticed, this should theoretically attempt to set up port again and again in case of an error. I limited it to port 9000, but you can try to tinker a solution that better suits what you intended to do and adjust it in your specific use-case scenario.