2014-01-28 87 views
1

所以即时尝试在基于Felix的OSGi包中使用Maven创建远程Rest(JSON)服务。Felix OSGi容器内的Spring Rest Json服务

我的基本服务接口:

@Controller 
@RequestMapping("/s/fileService") 
public interface RestFileService { 

    @RequestMapping(value = "/file", method = RequestMethod.POST) 
    @ResponseBody 
    public String getFile(Long id); 
} 

我接口的实现

public class RestFileServiceImpl implements RestFileService{ 

    public String getFile(Long id) { 
     return "test service"; 
    } 
} 

一般情况下我想补充一点,以我的web.xml

<servlet> 
     <servlet-name>spring-mvc-dispatcher</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <init-param> 
      <param-name>contextConfigLocation</param-name> 
      <param-value>/WEB-INF/application-context.xml</param-value> 
     </init-param> 
     <load-on-startup>1</load-on-startup> 
</servlet> 

<servlet-mapping> 
     <servlet-name>spring-mvc-dispatcher</servlet-name> 
     <url-pattern>/rest/*</url-pattern> 
</servlet-mapping> 

,这将很好地工作在一个正常的webapp中。 但现在我想把它放在一个OSGi包中。

的Servlet 3.0允许您使用@WebServlet来声明一个servlet没有 所以我创建了一个RestServlet

@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"); 
} 
} 

这是我的OSGi激活web.xml中:

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"); 
} 

} 

我知道费利克斯有JAX自己的HTTP实现,但我试图用Spring注释和尽可能少的XML来做到这一点。 我可以强制注册注释驱动的3.0 servlet吗?

我在做什么错了?这甚至有可能吗?

回答