1

I'm trying to expose a REST service through OSGi (using Apache Felix). I'm using the osgi-jax-rs-connector to publish the resource. Here is the resource interface:

@Path("/bingo")
public interface BingoService {

@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/lottery")
List<Integer> getLottery();
}

The implementation uses DS annotation to obtain reference to a provided service in container:

@Component(
    immediate = true,
    service = BingoService.class,
    properties = "jersey.properties")
public class Bingo implements BingoService {

@Reference
private RandomNumberGenerator rng;

@Activate
public void activateBingo() {
    System.out.println("Bingo Service activated using " +
                        rng.getClass().getSimpleName());
}

@Override
public List<Integer> getLottery() {
    List<Integer> result = new ArrayList<>();
    for (int i = 5; i > 0; i--) {
        result.add(rng.nextInt());
        }
    return result;
    }
}

jersey.properties simply contains this line

service.exported.interfaces=*

When I deploy the bundle it starts and register the service correctly. But if I go to http://localhost:8181/services/bingo/lottery I get 404. Could someone point me to the issue or give me some advice on where to look?

1 Answers1

0

On reading the documentation for OSGi - JAX-RS Connector, it expects to find the annotations @Path or @Provider on the service instance object. You have placed them instead on an interface implemented by the component.

I'm not sure what the purpose of the BingoService interface is. This is not required for JAX-RS services. Normally you would register the resource class using its own type (e.g. service=Bingo.class) or simply java.lang.Object.

Neil Bartlett
  • 23,743
  • 4
  • 44
  • 77
  • Unfortunately, also putting the annotation directly on service doesn't work. As a note: I'm pretty sure that annotations are present at runtime when the class is constructed so it's actually the same to put them on the interface. – user3657495 Mar 05 '18 at 15:35
  • Annotations are not inherited, so it's certainly the same to put them on the interface. However some frameworks may choose to walk the inheritance hierarchy when looking for them. You're correct to say that the JAX-RS annotations have runtime retention... JAX-RS would not work at all without this! – Neil Bartlett Mar 05 '18 at 16:52
  • Thanks for having specified this. I changed from Jersey to cxf and it started working. Anyhow it would be interesting to know why it's not working with previous solution. – user3657495 Mar 07 '18 at 11:10