Jersey extension installer¶
CoreInstallersBundle / JerseyProviderInstaller
Installs various jersey extensions, usually annotated with jersey @Provider
annotation and installed via environment.jersey().register()
:
Supplier, ExceptionMapper, ValueParamProvider, InjectionResolver,
ParamConverterProvider, ContextResolver, MessageBodyReader, MessageBodyWriter,
ReaderInterceptor, WriterInterceptor, ContainerRequestFilter,
ContainerResponseFilter, DynamicFeature, ApplicationEventListener
Recognition¶
Detects classes annotated with jersey @javax.ws.rs.ext.Provider
annotation and register their instances in jersey.
Extensions registered as singletons, when no explicit scope annotation is used. Behaviour could be disabled with option:
.option(InstallerOptions.ForceSingletonForJerseyExtensions, false)
Special @Protptype
scope annotation may be used to mark resources in prototype scope.
It is useful when guice servlet support is disabled (and so @RequestScoped
could not be used).
Due to specifics of HK2 integration, you may need to use:
@JerseyManaged
to delegate bean creation to HK2@LazyBinding
to delay bean creation to time when all dependencies will be availablejavax.inject.Provider
as universal workaround (to wrap not immediately available dependency).
Or you can enable HK2 management for jersey extensions by default. Note that this will affect resources too and guice aop will not work on jersey extensions.
Supplier¶
Warning
Supplier
is used now by hk2 as a replacement to it's own Factory
interface.
If you were using AbstractContainerRequestValueFactory
then use just Supplier<T>
instead.
Any class implementing java.util.function.Supplier
(or extending abstract class implementing it).
public class MySupplier implements Supplier<MyModel> {
@Override
public MyModel get() {
...
}
}
Suppliers in essence are very like guice (or javax.inject
) providers (Provider
).
Warning
Previously, factories were used as auth objects providers. Now Function<ContainerRequest, ?>
must be used instead:
@Provider
class AuthFactory implements Function<ContainerRequest, User> {
@Override
public User apply(ContainerRequest containerRequest) {
return new User();
}
}
ExceptionMapper¶
Any class implementing javax.ws.rs.ext.ExceptionMapper
(or extending abstract class implementing it).
Useful for error handling customization.
@Provider
public class DummyExceptionMapper implements ExceptionMapper<RuntimeException> {
private final Logger logger = LoggerFactory.getLogger(DummyExceptionMapper.class);
@Override
public Response toResponse(RuntimeException e) {
logger.debug("Problem while executing", e);
return Response.status(Response.Status.BAD_REQUEST)
.type(MediaType.TEXT_PLAIN)
.entity(e.getMessage())
.build();
}
}
Tip
You can also use ExtendedExceptionMapper
as more flexible alternative. See example usage in
dropwizard-views.
Tip
Default exception dropwizard mappers (registered in io.dropwizard.setup.ExceptionMapperBinder
) could be
overridden
or completely disabled with server.registerDefaultExceptionMappers
option.
ValueParamProvider¶
Any class implementing org.glassfish.jersey.server.spi.internal.ValueParamProvider
(or extending abstract class implementing it).
@Provider
public class AuthFactoryProvider extends AbstractValueParamProvider {
private final AuthFactory authFactory;
@Inject
public AuthFactoryProvider(final javax.inject.Provider<MultivaluedParameterExtractorProvider> extractorProvider,
final AuthFactory factory) {
super(extractorProvider, Parameter.Source.UNKNOWN);
this.authFactory = factory;
}
@Override
protected Function<ContainerRequest, User> createValueProvider(Parameter parameter) {
final Auth auth = parameter.getAnnotation(Auth.class);
return auth != null ? authFactory : null;
}
}
InjectionResolver¶
Any class implementing org.glassfish.hk2.api.InjectionResolver
(or extending abstract class implementing it).
@Provider
class MyObjInjectionResolver implements InjectionResolver<MyObjAnn> {
@Override
public Object resolve(Injectee injectee) {
return new MyObj();
}
@Override
public Class<MyObjAnn> getAnnotation() {
return MyObjAnn.class;
}
@Override
public boolean isConstructorParameterIndicator() {
return false;
}
@Override
public boolean isMethodParameterIndicator() {
return true;
}
}
ParamConverterProvider¶
Any class implementing javax.ws.rs.ext.ParamConverterProvider
(or extending abstract class implementing it).
@Provider
public class FooParamConverter implements ParamConverterProvider {
@Override
public <T> ParamConverter<T> getConverter(Class<T> rawType, Type genericType, Annotation[] annotations) {
if (Foo.class.isAssignableFrom(rawType)) {
return (ParamConverter<T>) new FooConverter();
}
return null;
}
private static class FooConverter implements ParamConverter<Foo> {
@Override
public Foo fromString(String value) {
return new Foo(value);
}
@Override
public String toString(Foo value) {
return value.value;
}
}
}
ContextResolver¶
Any class implementing javax.ws.rs.ext.ContextResolver
(or extending abstract class implementing it).
@Provider
public class MyContextResolver implements ContextResolver<Context> {
@Override
public Context getContext(Class type) {
return new Context();
}
public static class Context {}
}
MessageBodyReader¶
Any class implementing javax.ws.rs.ext.MessageBodyReader
(or extending abstract class implementing it).
Useful for custom representations.
@Provider
public class TypeMessageBodyReader implements MessageBodyReader<Type> {
@Override
public boolean isReadable(Class<?> type, java.lang.reflect.Type genericType, Annotation[] annotations, MediaType mediaType) {
return false;
}
@Override
public Type readFrom(Class<Type> type, java.lang.reflect.Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException, WebApplicationException {
return null;
}
public static class Type {}
}
MessageBodyWriter¶
Any class implementing javax.ws.rs.ext.MessageBodyWriter
(or extending abstract class implementing it).
Useful for custom representations.
@Provider
public class TypeMessageBodyWriter implements MessageBodyWriter<Type> {
@Override
public boolean isWriteable(Class<?> type, java.lang.reflect.Type genericType, Annotation[] annotations, MediaType mediaType) {
return false;
}
@Override
public long getSize(Type type, Class<?> type2, java.lang.reflect.Type genericType, Annotation[] annotations, MediaType mediaType) {
return 0;
}
@Override
public void writeTo(Type type, Class<?> type2, java.lang.reflect.Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException {
}
public static class Type {}
}
ReaderInterceptor¶
Any class implementing javax.ws.rs.ext.ReaderInterceptor
(or extending abstract class implementing it).
@Provider
public class MyReaderInterceptor implements ReaderInterceptor {
@Override
public Object aroundReadFrom(ReaderInterceptorContext context) throws IOException, WebApplicationException {
return null;
}
}
WriterInterceptor¶
Any class implementing javax.ws.rs.ext.WriterInterceptor
(or extending abstract class implementing it).
@Provider
public class MyWriterInterceptor implements WriterInterceptor {
@Override
void aroundWriteTo(WriterInterceptorContext context) throws IOException, WebApplicationException {
}
}
ContainerRequestFilter¶
Any class implementing javax.ws.rs.container.ContainerRequestFilter
(or extending abstract class implementing it).
Useful for request modifications.
@Provider
public class MyContainerRequestFilter implements ContainerRequestFilter {
@Override
public void filter(ContainerRequestContext requestContext) throws IOException {
}
}
ContainerResponseFilter¶
Any class implementing javax.ws.rs.container.ContainerResponseFilter
(or extending abstract class implementing it).
Useful for response modifications.
@Provider
public class MyContainerResponseFilter implements ContainerResponseFilter {
@Override
public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException {
}
}
DynamicFeature¶
Any class implementing javax.ws.rs.container.DynamicFeature
(or extending abstract class implementing it).
Useful for conditional activation of filters.
@Provider
public class MyDynamicFeature implements DynamicFeature {
@Override
public void configure(ResourceInfo resourceInfo, FeatureContext context) {
}
}
ApplicationEventListener¶
Any class implementing org.glassfish.jersey.server.monitoring.ApplicationEventListener
(or extending abstract class implementing it).
@Provider
public class MyApplicationEventListener implements ApplicationEventListener {
@Override
public void onEvent(ApplicationEvent event) {
}
@Override
public RequestEventListener onRequest(RequestEvent requestEvent) {
return null;
}
}