Skip to content

Commit 510cc8e

Browse files
committed
Fix: 'Service not found' error on an mvc controller mounted over sub-application fix jooby-project#2457
1 parent a184142 commit 510cc8e

File tree

9 files changed

+137
-12
lines changed

9 files changed

+137
-12
lines changed

jooby/src/main/java/io/jooby/Jooby.java

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -298,16 +298,6 @@ public Router setLocales(Locale... locales) {
298298
return router.getContextPath();
299299
}
300300

301-
@Nonnull @Override
302-
public Jooby mount(@Nonnull Router router) {
303-
this.router.mount(router);
304-
if (router instanceof Jooby) {
305-
Jooby child = (Jooby) router;
306-
child.registry = this.registry;
307-
}
308-
return this;
309-
}
310-
311301
/**
312302
* Installs/imports a full application into this one. Applications share services, registry,
313303
* callbacks, etc.
@@ -323,7 +313,7 @@ public Jooby mount(@Nonnull Router router) {
323313
*
324314
* }</pre>
325315
*
326-
* Lazy creation allows to configure and setup <code>SubApp</code> correctly, the next example
316+
* Lazy creation configures and setup <code>SubApp</code> correctly, the next example
327317
* won't work:
328318
*
329319
* <pre>{@code
@@ -426,9 +416,18 @@ public Jooby mount(@Nonnull Predicate<Context> predicate, @Nonnull Router subrou
426416

427417
@Nonnull @Override public Jooby mount(@Nonnull String path, @Nonnull Router router) {
428418
this.router.mount(path, router);
419+
if (router instanceof Jooby) {
420+
Jooby child = (Jooby) router;
421+
child.registry = this.registry;
422+
}
429423
return this;
430424
}
431425

426+
@Nonnull @Override
427+
public Jooby mount(@Nonnull Router router) {
428+
return mount("/", router);
429+
}
430+
432431
@Nonnull @Override public Jooby mvc(@Nonnull Object router) {
433432
Provider provider = () -> router;
434433
return mvc(router.getClass(), provider);

jooby/src/main/java/io/jooby/internal/RouterImpl.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@
5858
import io.jooby.ServiceRegistry;
5959
import io.jooby.SessionStore;
6060
import io.jooby.StatusCode;
61-
import io.jooby.TemplateEngine;
6261
import io.jooby.ValueConverter;
6362
import io.jooby.WebSocket;
6463
import io.jooby.exception.RegistryException;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package io.jooby.i2457;
2+
3+
import javax.inject.Inject;
4+
5+
import io.jooby.annotations.GET;
6+
import io.jooby.annotations.Path;
7+
8+
@Path("/")
9+
public class ControllerV12457 {
10+
11+
@Inject
12+
private WelcomeService2457 welcomeService;
13+
14+
@GET("/welcome")
15+
public String sayHi() {
16+
return welcomeService.welcome("v1");
17+
}
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package io.jooby.i2457;
2+
3+
import javax.inject.Inject;
4+
5+
import io.jooby.annotations.GET;
6+
import io.jooby.annotations.Path;
7+
8+
@Path("/")
9+
public class ControllerV22457 {
10+
11+
@Inject
12+
private WelcomeService2457 welcomeService;
13+
14+
@GET("/welcome")
15+
public String sayHi() {
16+
return welcomeService.welcome("v2");
17+
}
18+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package io.jooby.i2457;
2+
3+
import io.jooby.Jooby;
4+
5+
public class ControllersAppV12457 extends Jooby {
6+
7+
public ControllersAppV12457() {
8+
mvc(ControllerV12457.class);
9+
}
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package io.jooby.i2457;
2+
3+
import io.jooby.Jooby;
4+
5+
public class ControllersAppV22457 extends Jooby {
6+
7+
public ControllersAppV22457() {
8+
mvc(ControllerV22457.class);
9+
}
10+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package io.jooby.i2457;
2+
3+
import javax.inject.Inject;
4+
5+
import com.google.common.collect.ImmutableMap;
6+
import io.jooby.Context;
7+
import io.jooby.annotations.GET;
8+
import io.jooby.annotations.Path;
9+
10+
@Path("/")
11+
public class HealthController2457 {
12+
13+
@Inject
14+
private WelcomeService2457 welcomeService;
15+
16+
@GET("/healthcheck")
17+
public void healthCheck(Context ctx) {
18+
String welcome = welcomeService.welcome("healthcheck");
19+
ctx.setResponseCode(200)
20+
.render(ImmutableMap.of(
21+
"status", "Ok",
22+
"welcome", welcome)
23+
);
24+
}
25+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package io.jooby.i2457;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import io.jooby.di.GuiceModule;
6+
import io.jooby.junit.ServerTest;
7+
import io.jooby.junit.ServerTestRunner;
8+
9+
public class Issue2457 {
10+
11+
@ServerTest
12+
public void shouldFindServiceOnMountedApp(ServerTestRunner runner) {
13+
runner.define(app -> {
14+
app.install(new GuiceModule());
15+
16+
app.mvc(HealthController2457.class);
17+
18+
app.mount("/api/v1", new ControllersAppV12457());
19+
app.mount("/api/v2", new ControllersAppV22457());
20+
21+
}).ready(http -> {
22+
http.get("/healthcheck", rsp -> {
23+
assertEquals(200, rsp.code());
24+
assertEquals("{status=Ok, welcome=[API healthcheck] Welcome Jooby!}", rsp.body().string());
25+
});
26+
27+
http.get("/api/v1/welcome", rsp -> {
28+
assertEquals(200, rsp.code());
29+
assertEquals("[API v1] Welcome Jooby!", rsp.body().string());
30+
});
31+
32+
http.get("/api/v2/welcome", rsp -> {
33+
assertEquals(200, rsp.code());
34+
assertEquals("[API v2] Welcome Jooby!", rsp.body().string());
35+
});
36+
});
37+
}
38+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package io.jooby.i2457;
2+
3+
public class WelcomeService2457 {
4+
5+
public String welcome(String version) {
6+
return "[API " + version + "] Welcome Jooby!";
7+
}
8+
}

0 commit comments

Comments
 (0)