I want to create and deploy a web service to OSGi container. For example, publish the service to the address:
http://localhost:8080/testservice.
The service generate HTML response in a servlet.
I have searched a lot and got:
public class HelloWorldServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>Hola</title>");
out.println("</head>");
out.println("<body bgcolor=\"white\">");
out.println("</body>");
out.println("</html>");
}
}
The tool I need to use:
maven to create the project
Fuse ESB karaf as OSGi container
The question is that I do not know how to use Maven to create and implement such web service, like:
how to specify webapp/web.xml
how to specify pom.xml
: dependencies, package type, plugin
how to register service: implement BundlActivator
or configure Spring xml file
Can anyone help me with this? Is there a detailed tutorial for newbie?
A Servlet is a class used to extend the capabilities of servers that host applications accessed by means of a request-response programming model.
OSGi is a Java framework for developing and deploying modular software programs and libraries. Each bundle is a tightly coupled, dynamically loadable collection of classes, jars, and configuration files that explicitly declare their external dependencies (if any).
Designate — Generate a Designate element in the Meta Type Resource for an ObjectClassDefinition using the annotated Declarative Services component. Define a Designate to the servlet to the ObjectClassDefinition. Now the servlet is registered with configured resource types, selectors, and extensions.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With