Skip to content

Instantly share code, notes, and snippets.

@dmorgantini
Last active August 13, 2020 20:09
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmorgantini/11397642 to your computer and use it in GitHub Desktop.
Save dmorgantini/11397642 to your computer and use it in GitHub Desktop.
Dropwizard App with Admin Resource
public class TemplateApplication extends Application<ServiceConfiguration> {
public static void main(String[] args) throws Exception {
new TemplateApplication().run(args);
}
@Override
public void run(ServiceConfiguration configuration, Environment environment) throws Exception {
environment.jersey().register(new HelloWorldResource());
final DropwizardResourceConfig jerseyConfig = new DropwizardResourceConfig(environment.metrics());
JerseyContainerHolder jerseyContainerHolder = new JerseyContainerHolder(new ServletContainer(jerseyConfig));
jerseyConfig.getSingletons().add(new AdminResource());
environment.admin().addServlet("admin resources", jerseyContainerHolder.getContainer()).addMapping("/admin/*");
}
@Override
public void initialize(Bootstrap<ServiceConfiguration> bootstrap) {
}
}
Copy link

ghost commented Apr 30, 2014

Where is AdminResource?

@Maddoc42
Copy link

In case anyone else comes along looking for this, in newer versions of Dropwizard adding resources to jerseyConfig.getSingletons() does not work, but registering them does.

jerseyConfig.register(injector.getInstance(AdminFilterChainApi.class));

Tested with Dropwizard version 0.8.0-rc1

@piersy
Copy link

piersy commented Jan 23, 2015

Hi Maddoc42 can I ask what is the injector in your snippet, is it something provided by dropwizard?

@OzWolf
Copy link

OzWolf commented Jan 29, 2015

Hi piersy, the injector Maddoc is using is most likely a Guice injector instance, as DropWizard + Guice is a very common combination.

@ferdy-lw
Copy link

ferdy-lw commented Feb 2, 2015

I pretty much did it the same way for DW 0.8, but add the jersey providers to the environment and registered through the environment (without an injector).

        // Admin Pages
        //============
        final DropwizardResourceConfig jerseyConfig = new DropwizardResourceConfig(environment.metrics());
        JerseyContainerHolder jerseyContainerHolder = new JerseyContainerHolder(new ServletContainer(jerseyConfig));
        JerseyEnvironment jerseyEnvironment = new JerseyEnvironment(jerseyContainerHolder, jerseyConfig);

        // Providers - Can use DW View responses
        jerseyEnvironment.register(new JacksonMessageBodyProvider(environment.getObjectMapper(), environment.getValidator()));
        jerseyEnvironment.register(new ViewMessageBodyWriter(environment.metrics(), ImmutableSet.copyOf(ServiceLoader.load(ViewRenderer.class))));

        // Specific service pages
        jerseyEnvironment.register(new Facets(assets));
        // Top level admin summary page
        jerseyEnvironment.register(new Admin(jerseyEnvironment));

        // Attach the admin servlet
        environment.admin().addServlet("admin resources", jerseyContainerHolder.getContainer()).addMapping("/admin/*");

        // Grab the regular host and port, only so we can test the API (usu. 8080) from the admin connector pages (port 8081)
        environment.lifecycle().addServerLifecycleListener(s ->
                        Stream.of(s.getConnectors())
                                .filter(c -> "application".equals(c.getName()))
                                .forEach(c -> {

                                    ServerConnector connector = (ServerConnector) c;

                                    // assets - simple singleton of system configuration
                                    assets.setAppHostName(connector.getHost() == null ? "127.0.0.1" : connector.getHost());
                                    assets.setAppPort(connector.getLocalPort() <= 0 ? connector.getPort() : connector.getLocalPort());
                                })
        );

@Vandalko
Copy link

as a person who recently hit into this issue - need to configure Admin space servlet, I recommend using https://github.com/xvik/dropwizard-guicey which can automatically wire up your annotated servlets

@rajneeshpatel
Copy link

Used @ferdy-lw approach and worked fine with 1.0.5. As I have a separate admin context configured using the simple server config, used the mapping for the servlet as below to not conflict with the regular admin servlet.

environment.admin().addServlet("admin jersey resources", jerseyContainerHolder.getContainer()).addMapping("/admin/api/*");

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment