cxf-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From marceli <MarceliNar...@gmail.com>
Subject Host CXF REST Service + Static Web Content on Same Embedded Server?
Date Fri, 22 Oct 2010 20:23:29 GMT

How do I host static web content (HTML/JavaScript/images) and a CXF REST
service on the same port through the same embedded web server?

I can host just the CXF REST service on an embedded Jetty server like this:

	public static void jaxrs() throws Exception {
		System.out.println("Starting JAX-RS server...");

		JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
		sf.setResourceClasses(CustomerService.class);
		sf.setAddress("http://localhost:9001/");
		org.apache.cxf.endpoint.Server server = sf.create();

		server.start();

		System.out.println("Started...");
	}

I can alternatively host just static web content on an embedded Jetty server
like this:

	public static void serveStatic() throws Exception {
		org.eclipse.jetty.server.Server server = new
org.eclipse.jetty.server.Server();
		SelectChannelConnector connector = new SelectChannelConnector();
		connector.setPort(8080);
		server.addConnector(connector);

		ResourceHandler resource_handler = new ResourceHandler();
		resource_handler.setDirectoriesListed(true);
		resource_handler.setWelcomeFiles(new String[]{"index.html"});

		//resource_handler.setResourceBase(".");
		resource_handler.setResourceBase("C:\\staticwebdir");

		HandlerList handlers = new HandlerList();
		handlers.setHandlers(new Handler[]{resource_handler, new
DefaultHandler()});
		server.setHandler(handlers);

		server.start();
		server.join();
	}

But how can I host both simultaneously on the same embedded server?

The problem with hosting them separately is that my static JavaScript files
are trying to make XmlHttpRequest calls to the REST service and that doesn't
work across different ports. Both client-side JavaScript and the REST
service must be hosted on the same port, hence they must be hosted through
the same embedded server.

The alternative is to use a non-embedded standalone server, but I don't want
to sacrifice the simplicity and speed that the embedded server approach
offers.
-- 
View this message in context: http://cxf.547215.n5.nabble.com/Host-CXF-REST-Service-Static-Web-Content-on-Same-Embedded-Server-tp3232898p3232898.html
Sent from the cxf-user mailing list archive at Nabble.com.

Mime
View raw message