繁体   English   中英

Felix OSGi容器中的Spring Rest Json Service

[英]Spring Rest Json Service inside a Felix OSGi container

因此,我试图在基于Felix和Maven的OSGi捆绑包中创建远程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来声明没有web.xml的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激活器:

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

}

我知道Felix使用JAX拥有自己的http实现,但是我试图通过spring注释和尽可能少的XML来实现。 我可以强制其注册注释驱动的3.0 servlet吗?

我究竟做错了什么 ? 这可能吗?

如果您正在寻找一种在OSGi中进行REST的简便方法,请看一下Amdatu项目提供的一些Web组件。 该页面几乎解释了如何创建REST服务: https : //amdatu.org/application/web/ ,还有一个视频可以指导您完成整个过程: https : //amdatu.org/generaltop/videolessons /

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM