Handling the root URI using SimpleServerFactory

34 views
Skip to first unread message

Joe Barnett

unread,
May 5, 2020, 8:36:11 PM5/5/20
to dropwizard-user
Hello,

We're using SimpleServerFactory and running on heroku, and would like the root url (eg https://my-dropwizard-app.herokuapp.com ) to redirect to the applicationContextPath (eg https://my-dropwizard-app.herokuapp.com/application ) rather than always return a 404.  It looks like this can be done by subclassing SimpleServerFactory and overriding the `build` method to copy/paste everything with an additional `ContextRoutingHandler` map entry that handles "/" with a Handler that returns a 302 redirect to the applicationContextPath, but would prefer to not have to duplicate the `build` method logic.  Is there a hook that we're missing that would solve this issue differently?  If not, would it make sense to add a way for applications to configure the SimpleServerFactory to handle the root URI?

Thanks,
-Joe

Bobby Johansen

unread,
May 6, 2020, 10:50:51 AM5/6/20
to dropwiz...@googlegroups.com
This sounds like a good use case for nginx or a reverse proxy rather than dropwizard. It would be a bit odd for dropwizard to redirect the domain root context to its application context as there could be and typically are other services living behind a single domain. I am by no means a Heroku expert but it feels like the responsibility of the root domain should live with something on the network layer rather than the application layer.

--Bobby Johansen

Sent via Superhuman


--
You received this message because you are subscribed to the Google Groups "dropwizard-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to dropwizard-user+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/dropwizard-user/d916938d-cd43-425b-867e-e1ec6e80d864%40googlegroups.com.

Dimas Guardado

unread,
May 8, 2020, 1:23:58 AM5/8/20
to dropwizard-user
One way to implement this is to mount the application context at "/" and the jersey root at "/application", then registering a redirect servlet at "/"

```config.yml
server:
  type: simple
  applicationContextPath: /
  rootPath: /application
  adminContextPath: /admin
```

```YourApplication.java
    @Override
    public void run(final SampleContextConfiguration configuration,
                    final Environment environment) {
        environment.servlets().addServlet("root-redirect", new HttpServlet() {
            @Override
            protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
                resp.sendRedirect("/application");
            }
        }).addMapping(""); // empty string maps to root path only, "/" will map to any unmapped path
    }
```

Does that do what you're looking for?

-Dimas

Joe Barnett

unread,
May 9, 2020, 4:47:34 PM5/9/20
to dropwizard-user
That does, thanks!
Reply all
Reply to author
Forward
0 new messages