REST Server only in Eclipse SmartHome possible?

Hello,
I want to make my own REST Server for example like the DiscoveryResource.java in org.eclipse.smarthome.io.rest.core. For example class I implemented the RESTResource and created the resource.xml. In the Eclipse SmartHome namespace the example works fine. When I copy the .java and .xml in the openHAB2 namespace and change the packagenames, it doesn’t work anymore. What am I doing wrong?

The following is my example which only works in the Eclipse SmartHome namespace:

package org.eclipse.smarthome.io.rest.core.discovery;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import org.eclipse.smarthome.io.rest.RESTResource;/**

@Path("myowntestpath")
public class MessageResource implements RESTResource {    
    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String getDiscoveryServices() {
        return "hi";
    }}

my xml File:

<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="org.eclipse.smarthome.io.rest.core.discovery.MessageResource">
   <implementation class="org.eclipse.smarthome.io.rest.core.discovery.MessageResource"/>
   <service>
      <provide interface="org.eclipse.smarthome.io.rest.core.discovery.MessageResource"/>
      <provide interface="org.eclipse.smarthome.io.rest.RESTResource"/>
   </service>
</scr:component>

Solved, I had to edit the MANIFEST.MF.

1 Like

Yes, you are on a good way. In general, there are two things important to know:

  1. The REST API uses JAX-RS 2.0 and as such one has to annotate the class accordingly.
  2. The Resource class must implement the interface RESTResource and must be registered with this interface as an OSGi service (what can be done through OSGi Declarative Services as in your example).

Regards,
Kai

2 Likes

Hi ,

When I created multiple Resource classes say like Class A implements RESTResource again Class B implements RESTResource and so on and also created respective XML (service component as DS) and added to MANIFEST.MF.

Only one Resource class get RESTResource service. All other shows Not Found Exception (404).

MANIFEST.MF service component goes as below ,


Service-Component: OSGI-INF/A.xml, OSGI-INF/B.xml


Please help.No idea why my resource class not get registered.

Service component definition description has a field called ‘name’ which defines the name of the service component. Accidentally, I put the same name to both service component (A.xml and B.xml) which caused duplication. Resolve once when i put the different name.