Is it possible to use the @WebServlet
annotation with Olingo? I want to avoid creating the entries in our web.xml
:
<servlet>
<servlet-name>CarServiceServlet</servlet-name>
<servlet-class>org.apache.cxf.jaxrs.servlet.CXFNonSpringJaxrsServlet</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>org.apache.olingo.odata2.core.rest.app.ODataApplication</param-value>
</init-param>
<init-param>
<param-name>org.apache.olingo.odata2.service.factory</param-name>
<param-value>com.sample.CarServiceFactory</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CarServiceServlet</servlet-name>
<url-pattern>/CarService.svc/*</url-pattern>
</servlet-mapping>
The code is taken from the olingo-sample
repository. Perhaps there is a way like @WebInitParams
to create the init parameters, but what I have to annotate - the class X extends ODataServiceFactory
? Does it work generelly?
EDIT: I am using Olingo V2 and Tomcat 8.
Sure you can do it with the annotation. You will have to create a class that extends from CXFNonSpringJaxrsServlet
and pass the init parameters in @WebServlet
annotation.
Following is the code that replicates your web.xml
.
package com.sample;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import org.apache.cxf.jaxrs.servlet.CXFNonSpringJaxrsServlet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author Shiva Saxena
*
*/
@WebServlet(name = "CarServiceServlet", value = "/CarService.svc/*", initParams = {
@WebInitParam(name = "javax.ws.rs.Application", value = "org.apache.olingo.odata2.core.rest.app.ODataApplication"),
@WebInitParam(name = "org.apache.olingo.odata2.service.factory", value = "com.sample.CarServiceFactory") })
public class MyServlet extends CXFNonSpringJaxrsServlet {
Logger logger = LoggerFactory.getLogger(MyServlet.class);
private static final long serialVersionUID = -5663461069269732798L;
@Override
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
// It is not necessory to override this function.Its here just for logging and better understanding
logger.info("Call intercepted by:" + this.getClass().getName());
super.service(req, res);
}
}