This repository will no longer be maintained except for critical security fixes and should not be used for new development.
It is superseded by Soklet 2.0.0, which no longer provides formal support for Guice (it is now simple to bring-your-own DI).
- No new features or bug fixes will be made except for critical security fixes
- Issues and pull requests are not monitored
- The repository is kept for historical reference only
Replacement: https://github.com/soklet/soklet
Final release: 1.0.6 (January 26, 2019)
Guice integration for Soklet, a minimalist infrastructure for Java webapps and microservices.
<dependency>
<groupId>com.soklet</groupId>
<artifactId>soklet-guice</artifactId>
<version>1.0.6</version>
</dependency>If you don't use Maven, you can drop soklet-guice-1.0.6.jar directly into your project. You'll also need Guice 4.0 as a dependency.
// SokletModule provides default implementations for Soklet interfaces like ResponseHandler.
// You can override them in your own module as needed
public static void main(String[] args) throws Exception {
Injector injector = createInjector(Modules.override(new SokletModule()).with(new AppModule()));
Server server = injector.getInstance(Server.class);
server.start();
System.in.read(); // Wait for keypress
server.stop();
}
class AppModule extends AbstractModule {
// SokletMatchers.httpMethodMatcher() is a Guice Matcher which matches any resource method.
// For example, methods annotated with @GET("/test") and @DELETE("/users/{id}") would be matched.
// This is useful for applying method interceptors to handle security, database transactions, and more
@Override
protected void configure() {
bindInterceptor(Matchers.annotatedWith(Resource.class),
SokletMatchers.httpMethodMatcher(), new MyCustomInterceptor());
}
// Use Guice as you would normally
@Provides
@Singleton
public Server provideServer(InstanceProvider instanceProvider) {
return JettyServer.forInstanceProvider(instanceProvider).port(8080).build();
}
// Override Soklet's default implementations as needed
@Provides
@Singleton
public PageResponseWriter providePageResponseWriter() {
// My app should use a custom Mustache.java-backed page response writer
return new MyMustachePageResponseWriter();
}
}Soklet Guice was created by Mark Allen and sponsored by Transmogrify, LLC.