title: Spring Rest Json Service inside a Felix OSGi container
body: <p>So im trying to create a remote Rest (JSON) service inside an OSGi bundle based in Felix with Maven.</p>
  
  <p>my basic service interface :</p>
  
  <pre><code>@Controller
  @RequestMapping("/s/fileService")
  public interface RestFileService {
  
     @RequestMapping(value = "/file", method = RequestMethod.POST)
     @ResponseBody
     public String getFile(Long id);
  }
  </code></pre>
  
  <p>My implementation of the interface</p>
  
  <pre><code>public class RestFileServiceImpl implements RestFileService{
  
      public String getFile(Long id) {
          return "test service";
      }
  }
  </code></pre>
  
  <p>Normally i would add this to my web.xml</p>
  
  <pre><code>&lt;servlet&gt;
        &lt;servlet-name&gt;spring-mvc-dispatcher&lt;/servlet-name&gt;
        &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
        &lt;init-param&gt;
             &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
             &lt;param-value&gt;/WEB-INF/application-context.xml&lt;/param-value&gt;
        &lt;/init-param&gt;
        &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
   &lt;/servlet&gt;
  
   &lt;servlet-mapping&gt;
        &lt;servlet-name&gt;spring-mvc-dispatcher&lt;/servlet-name&gt;
        &lt;url-pattern&gt;/rest/*&lt;/url-pattern&gt;
   &lt;/servlet-mapping&gt; 
  </code></pre>
  
  <p>And this would work fine inside a normal webapp.
  But now i want to put this inside an OSGi bundle.</p>
  
  <p>Servlet 3.0 allows you to use @WebServlet to declare a servlet without the web.xml
  So i created a RestServlet</p>
  
  <pre><code>@WebServlet(value="/rest", name="rest-servlet")
  public class RestServlet implements ServletContextListener {
  
  private static Log sLog = LogFactory.getLog(RestServlet.class);
  
  public void contextInitialized(ServletContextEvent arg0) {
      sLog.info("initializing the Rest Servlet");
  }
  
  public void contextDestroyed(ServletContextEvent arg0) {
      sLog.info("un-initializing the Rest Servlet");  
  }
  }
  </code></pre>
  
  <p>This is my OSGi activator:</p>
  
  <pre><code>public class Activator implements BundleActivator {
  
  private static Log sLog = LogFactory.getLog(Activator.class);
  
  public void start(BundleContext context) throws Exception {
  
      /*
       * Exposing the Servlet
       */
  
      Dictionary properties = new Hashtable();
      context.registerService(RestFileService.class.getName(), new  RestFileServiceImpl(), properties );
  
      sLog.info("Registered Remote Rest Service");
  }
  
  public void stop(BundleContext context) throws Exception {
      sLog.info("Unregistered Remote Rest Service");
  }
  
  }
  </code></pre>
  
  <p>I know Felix has its own http implementation with JAX but im trying to do this with spring annotations and as little XML as possible. 
  Can i force it to register the annotation driven 3.0 servlet ?</p>
  
  <p>What am i doing wrong ? is this even possible ?</p>