Hi
Right now it is not possible to pass an initialized instance directly to
the servlet, though I can see it can be handy in some cases.
In many cases one can still achieve the same with the current approach,
example, you can have Catalog properties passed too, Catalog(a=1 b=2),
see the example at the wiki. List properties can be passed too.
if you do need to pass some ready instance then you can either go with
working directlu with JAXRSServerFactoryBean (I'd prefer it instead of
dealing directly with Jetty handlers), use jaxrs:server with an absolute
HTTP address, or in this case, extend CxfNonSpringJaxrsServlet and
override its configureSingleton class or it it is more involved,
register a custom Application.
I'll add a constructor CxfNonSpringJaxrsServlet(Application) to make
things simpler too
Cheers, Sergey
On 18/01/15 13:15, Maarten Boekhold wrote:
Hi Sergey,
I don't think so. Based on the following line:
servletHolder.setInitParameter("jaxrs.serviceClasses",
Catalog.class.getName());
I believe that this example still completely manages the lifecycle of
the Catalog bean, eg it is CXF that creates an instance of Catalog. In
my case, I already have a fully initialized instance of Catalog that I
need to use.
Maarten
On 2015-01-18 17:05, Sergey Beryozkin wrote:
Hi
Can the following help ?
https://git-wip-us.apache.org/repos/asf?p=cxf.git;a=blob;f=distribution/src/main/release/samples/jax_rs/search/src/main/java/demo/jaxrs/search/server/Server.java;h=f0ce84e80229cd9ad2a497a0c9ab6b36a69872db;hb=HEAD
Sergey
On 18/01/15 12:27, Maarten Boekhold wrote:
Hi,
On 2015-01-18 13:03, Maarten Boekhold wrote:
I'm a bit confused still about the part where we tell the
ServletHolder (or CFXNonSpringJaxrsServler?) to dispatch incoming
requests to a specific object? The example I've seen of
CXFNonSpringJaxrsServlet uses
setInitParameter("javax.ws.rs.Application", "some.class.name"), but
that implies that CXF manages the lifecycle of the application object,
which won't work for me. I need to manage the lifecycle myself.
To demonstrate what I would like to do, I managed to implement this with
Jersey & Jetty. However as mentioned previously, I'd really like to be
able to do the same with CXF, because it just seems silly to have both
CXF (which I need for non-JAX-RS-reasons as well) as well as Jersey in
my project.
Code for Jersey+Jetty (note: works with Jersey 2.7, with 2.9 or 2.14 I'm
getting errors which I haven't figured out yet):
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.servlet.ServletContainer;
public class TheApp {
TheResource resource;
public TheApp(int port) throws Exception {
ServletContextHandler sch = new ServletContextHandler();
sch.setContextPath("/xxx");
resource = new TheResource();
ResourceConfig rc = new ResourceConfig();
rc.register(resource);
ServletContainer sc = new ServletContainer(rc);
ServletHolder holder = new ServletHolder(sc);
sch.addServlet(holder, "/*");
Server server = new Server(port);
server.setHandler(sch);
server.start();
server.join();
}
public static void main(String[] args) throws Exception {
TheApp app = new TheApp(8122);
}
}
"TheResource" is simplicity itself:
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Path("request")
@Produces(MediaType.TEXT_PLAIN)
public class TheResource {
private static final Logger LOG =
LoggerFactory.getLogger(TheResource.class);
public TheResource() {
LOG.info("Creating instance of TheResource");
}
@GET
@Path("{service:.*}")
@Produces("text/plain")
public String testit(@PathParam("service") final String
service) {
LOG.info("GET method called with service = {}", service);
return "Service = " + service;
}
}
Maarten