Last active
August 13, 2020 20:09
-
-
Save dmorgantini/11397642 to your computer and use it in GitHub Desktop.
Dropwizard App with Admin Resource
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | |
} | |
} |
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());
})
);
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
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
Hi piersy, the injector Maddoc is using is most likely a Guice injector instance, as DropWizard + Guice is a very common combination.