How to use servlet with OSGi

前端 未结 6 1378
悲&欢浪女
悲&欢浪女 2021-01-11 19:31

I want to create and deploy a web service to OSGi container. For example, publish the service to the address:

http://localhost:8080/testservice. 

6条回答
  •  没有蜡笔的小新
    2021-01-11 19:57

    If you use bndtools, create a Declarative Services project and add this annotation to your servlet:

     @Component(provide = Servlet.class, properties = {"alias=/hello"})
     public class HelloWorldServlet extends HttpServlet { ... }
    

    Then create a bnd run descriptor with 'Apache Felix 4 with Web Console and Gogo', just add the Apache Felix Http whiteboard bundle and you're good to go. You can find your servlet at http://localhost:8080/hello

    How it works. The @Component annotation makes your class a service (a Servlet service in this case due to the provide attribute). This is registered with the service property 'alias'. The Apache Felix Http Whiteboard bundle picks up these services and registers them as servlets. I do not think it can get any simpler than this.

提交回复
热议问题