Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run multiple web apps in one spring boot container

I would like to be able to have multiple web apps sharing a domain project and running under different contextPaths.

By setting server.contextPath=/webshop in a spring boot app I dont need to prefix all the RequestMappings.

I would like the webshop, admin, and main page to share a common domain project which contains all the entities and common services.

Maybe with something like?

public static void main(String[] args) {
    new SpringApplicationBuilder(Domain.class)
        .showBanner(false)
        .child(Admin.class, Webshop.class)
        .run(args);
}

My problem is how do I start a spring boot app with a common domain model, and then a couple of stand alone web apps with unique contextPaths?

like image 418
Leon Radley Avatar asked Sep 16 '14 11:09

Leon Radley


People also ask

Can we have 2 classes with @SpringBootApplication?

So yes, this is expected behavior given your project setup. Put each @SpringBootApplication class in a separate subpackage if you don't want this to happen for local testing.


1 Answers

Like this for example:

public static void main(String[] args) {
    start(Admin.class, Webshop.class).run(args);
    start(Another.class).properties("server.port=${other.port:9000}").run(args);
}

private static SpringApplicationBuilder start(Class<?>... sources) {
    return new SpringApplicationBuilder(Domain.class)
        .showBanner(false)
        .child(sources);
}

It would start two apps on different ports.

like image 96
Dave Syer Avatar answered Sep 21 '22 03:09

Dave Syer