Bndtools: Simple REST API test

95 views
Skip to first unread message

Rubén Pérez Vázquez

unread,
Oct 17, 2020, 3:43:39 PM10/17/20
to bndtool...@googlegroups.com
Hi all,

I've been really struggling with a very simple setup to have a REST "hello world". I followed the "OSGi enroute" tutorials back in the day, but I like the Bndtools workspace much better. However, it seems the OSGi enRoute tutorials had a lot of hidden "magic" (meaning that the Maven archetypes used there did a lot of the "heavy work" of figuring out which dependencies are needed), so I am not able to reproduce something functional using a Bndtools workspace model.

My setup is extremely simple. In a bundle, I have an interface:

import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
 
@Path("/test")
public interface TestService {
@GET
@Path("test")
@Produces(MediaType.TEXT_PLAIN)
public String test(@DefaultValue("Illusion") @QueryParam("name")String name);
}

, and the package-info.java file:

@org.osgi.annotation.bundle.Export
@org.osgi.annotation.versioning.Version("1.0.0")
package org.test.test.service.api;


The, in another bundle, I have an implementation:

@Component
public class TestServiceImpl implements TestService {
@Override
public String test(String name) {
return "Hello, " + name;
}

In yet another bundle, I have tried to create an app that puts the previous two bundles together. I assumed that simply adding the API bundle would pull the implementation, however, after resolving, my bnd.bnd file looks like this:

Rubén Pérez Vázquez

unread,
Oct 17, 2020, 3:47:38 PM10/17/20
to bndtool...@googlegroups.com
Hi,

I sent the previous email too soon. Sorry about the noise.

My bnd.bnd file looks like this after resolving:

-runrequires: \
bnd.identity;id='es.illusion.company.service.api',\
bnd.identity;id='org.apache.felix.gogo.command',\
bnd.identity;id='org.apache.felix.gogo.shell'
-runbundles: \
es.illusion.company;version=snapshot,\
es.illusion.company.service.api;version=snapshot,\
es.illusion.location;version=snapshot,\
es.illusion.person;version=snapshot,\
org.apache.aries.javax.jax.rs-api;version='[1.0.4,1.0.5)',\
org.apache.felix.gogo.command;version='[1.1.0,1.1.1)',\
org.apache.felix.gogo.runtime;version='[1.1.2,1.1.3)',\
org.apache.felix.gogo.shell;version='[1.1.2,1.1.3)'

 So it seems the connection between the API and the implementation bundles it not explicit enough. Apart from that, I know I'm not specifying that this should be a web resource, but I'm not really sure how. In short, I know some things are missing, but I just cannot figure out which, and how to properly add them.

Again, I'm sorry if this question is too basic. I know it is, but most of the documentation I can find in google does not really address my question.

Thanks for your patience, and sorry again for the double mail.

Best,
Rubén Pérez

scott...@gmail.com

unread,
Oct 17, 2020, 6:23:08 PM10/17/20
to bndtools-users
Hi Ruben,

Alas, I can't respond on behalf of Aries and it's dependencies (i.e. the api and implementation bundles).

However, I can point you to some bndtools project and bndrun templates that use the ECF JaxRS distribution provider based upon Jersey...which is part of ECF's Remote Services impl (which is chap 100 in OSGi compendium spec).   

These project templates are in this [1] ECF Remote Services workspace template.  If you create your own workspace based upon [1] then there are project templates for creating 'JaxRS Remote Service Hello API', 'JaxRS Remote Service Hello Impl', and 'JaxRS Remote Service Hello Consumer' projects.  As part of these project templates are bndrun files in the Impl and Consumer projects, and in these bndrun file are all the necessary dependencies for the Impl. 

The short story for the Impl and Consumer project dependencies is that the JaxRS APIs are necessary as compile time dependencies (e.g. to include the necessary JaxRS annotations in the service impl) and so are automatically included in the bndrun, but the distribution provider (i.e. Jersey and it's dependencies) that is responsible for runtime exporting of the JaxRS service impl must also be in the bndrun...along with the ECF RSA impl...so that the service is exported as per RSA spec when registered by DS (and then discovered and imported on the consumer...leading to DS injection).  The bndrun files that are created via the Impl and Consumer project templates have these dependencies as requirements (which when resolved by bndtools will produce a complete environment for exporting the remote service on impl and importing the remote service on consumer.

I expect a similar thing is necessary for including the Aries impl of the OSGi REST spec, but as I said above I don't know Aries well enough to tell you what is needed.

Thanks,

Scott

Tim Ward

unread,
Oct 18, 2020, 1:33:52 AM10/18/20
to bndtool...@googlegroups.com
Hi Ruben,

It looks like you are missing the @JaxrsResource annotation on your DS component. That does two things:

1. Adds the necessary “osgi.jaxrs.resource=true” service property. Without that your service will not be picked up by the whiteboard. 

2. Adds a requirement for the JAX-RS whiteboard implementation to your bundle. This causes an implementation to be pulled in when you resolve. 

You can add these things in separately, but it’s easier to use the annotation from the whiteboard specification. 

I hope this helps. 

All the best

Tim

Sent from my iPhone
--
You received this message because you are subscribed to the Google Groups "bndtools-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bndtools-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bndtools-users/CAFWAsK2e4KXJARRaQHjhtgw3u0M%2Bm2ir7Tr1dv%2BuKJW5e0um8Q%40mail.gmail.com.
Reply all
Reply to author
Forward
Message has been deleted
0 new messages