[英]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.