hstaudacher / osgi-jax-rs-connector

An OSGi - JAX-RS 2.0 Connector, software repository available on the link below
http://hstaudacher.github.io/osgi-jax-rs-connector
Other
190 stars 98 forks source link

Problem when decoupling the service declaration and implementation #109

Closed thanhlq closed 8 years ago

thanhlq commented 8 years ago

Today I am trying this example: com.eclipsesource.jaxrs.connector.example.ds, it worked well for me but I want to split the service declaration and implementation like this:

// Declaration of service

@Path("/product")
public interface IProductService {

    @GET
    @Path("/{id}")
    public Product getProductById(@PathParam("id") String id);

    @GET
    public String getAllProducts();
}

// Implementation

public class ProductService implements IProductService {

  private static Map<String, Product> simplisticProductDb = new HashMap<String, Product>();

  static {
    simplisticProductDb.put( "1", new Product( "Pencil", "Simple writing instrument" ) );
    simplisticProductDb.put( "2", new Product( "Roller", "Standard writing instrument" ) );
    simplisticProductDb.put( "3", new Product( "Foutain Pen", "Stylish writing instrument" ) );
  }

  @Override
  public Product getProductById( @PathParam( "id" ) String id ) {
    Product someProduct = simplisticProductDb.get( id );
    return someProduct;
  }

  @Override
  public String getAllProducts() {
    Gson gson = new Gson();
    return gson.toJson(simplisticProductDb.values());
  }
}

==> But only the second Get is working (getAllProducts), the getProductById is NOT working - I got HTTP 404 error.

Anyone has an idea?

Thank you very much!

(I am just brand-new to Jersey/osgi-jax-rs-connector.)

hstaudacher commented 8 years ago

2 things. Can you remove the @PathParam( "id" ) from the implementation? And second, are you sure you have registered the interface as a service and not the implementing class? Please post your component definition to get further feedback

thanhlq commented 8 years ago

Oh perfect!, thanks very much! I removed the @PathParam("id") and it worked.

My component defintion:

<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="ProductService">
   <implementation class="com.eclipsesource.jaxrs.connector.example.ds.provider.ProductService"/>
   <service>
      <provide interface="com.eclipsesource.jaxrs.connector.example.ds.api.IProductService"/>
   </service>
</scr:component>