Instead of scanning the package, you can add bindings to your implementation. MessageBodyWriter. For instance:
public class Config extends GuiceServletContextListener { @Override protected Injector getInjector() { return Guice.createInjector( new JerseyServletModule() { @Override protected void configureServlets() { bind(Service.class); bind(CsvWriter.class); serve("/services/*").with(GuiceContainer.class); } }); } }
where CsvWriter.java is as follows:
@Singleton @Produces("text/csv") @Provider public class CsvWriter implements MessageBodyWriter<Foo> { @Override public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) { return Foo.class.isAssignableFrom(type); } @Override public long getSize(Foo data, Class<?> type, Type genericType, Annotation annotations[], MediaType mediaType) { return -1; } @Override public void writeTo(Foo data, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> headers, OutputStream out) throws IOException {
and then enter some method in the service that @Produces ("text / csv").
source share