问题
Just spent a day trying to get Grizzly Static Content working. The following URL from the Grizzly Blog explained alot: Grizzly STatic Content .
I am trying to mimic Tomcat, in that I would the path to the static content to be below the webapp or the context handle.
public class SampleAdminApplication extends ResourceConfig {
public SampleAdminApplication() {
packages("com.companyname.sample.sampleadmin.server.services");
}
}
public class SampleGrizzlyWebServer {
public static void main(String[] args) throws IOException {
try {
HttpServer httpServer = GrizzlyHttpServerFactory.createHttpServer(
URI.create("http://localhost:9090/Sample/"));
/*--- Static Content ---*/
String jarPath = getJarPath(SampleGrizzlyWebServer.class);
CLStaticHttpHandler clStaticHttpHandler = new CLStaticHttpHandler(
new URLClassLoader(new URL[] {new File(jarPath).toURI().toURL()}),
"/", "/lib/", "/js/", "/css/");
ServerConfiguration sc = httpServer.getServerConfiguration();
sc.addHttpHandler(clStaticHttpHandler,"/SampleUI");
/*--- SampleAdmin WebappContext ---*/
WebappContext SampleAdminContext = new WebappContext("WebappContext", "/" + webapp + "/" + "SampleAdmin");
/*--- Servlet ---*/
final ResourceConfig sampleAdminRc = new SampleAdminApplication();
ServletRegistration sampleAdminRegistration = SampleAdminContext.addServlet("ServletContainer", new ServletContainer(sampleAdminRc));
sampleAdminRegistration.addMapping("/*");
/**
* Deploy Server
*/
SampleAdminContext.deploy(httpServer);
/**
* Start Server
*/
httpServer.start();
} catch (Exception ex) {
System.err.println("Error: " + ex.getMessage());
}
}
}
The above code works with the following URL's:
http://localhost:9090/Sample/SampleAdmin/restmethod
http://localhost:9090/SampleUI/hello.htm
However I would like the static pages to be below the webapp path "Sample" Like:
http://localhost:9090/Sample/UI/hello.htm
Any help would be appreciated.
回答1:
Well I don't know if I get any badges for answering my own question :) I worked for a couple of days, trying to implement a forward from a filter. There are multiple unanswered posts on this forum about jersey forwarding. Not obvious how to forward from a filter. EndRant
My solution was to use the deprecated call to HttpHandlerRegistration.builder() that allowed me to effectively set up a /Sample/UI context.
Now the code behaves like Tomcat where Sample/{SampleAdmin,UI} are the endpoints.
startServer("http://", "localhost", "Sample", "UI", "9090");
public static void startServer(String protocol, String host, String webapp, String ui, String port) {
try {
String BASE_URI = protocol + host + ":" + port + "/" + webapp + "/";
HttpServer httpServer = GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI));
/*--- Static UI Content ---*/
if(ui!=null && !ui.equals("")) {
String jarPath = getJarPath(SampleGrizzlyWebServer.class);
if (jarPath != null) {
CLStaticHttpHandler clStaticHttpHandler = new CLStaticHttpHandler(
new URLClassLoader(new URL[]{new File(jarPath).toURI().toURL()}),
"/", "/lib/", "/js/", "/css/");
ServerConfiguration sc = httpServer.getServerConfiguration();
sc.addHttpHandler(clStaticHttpHandler, HttpHandlerRegistration.builder().contextPath("/" + webapp + "/" + ui + "").urlPattern("").build());
}
}
/*--- SampleAdmin WebappContext ---*/
WebappContext SampleAdminContext = new WebappContext("WebappContext", "/" + webapp + "/" + "SampleAdmin");
/*--- Servlet ---*/
final ResourceConfig sampleAdminRc = new SampleAdminApplication();
ServletRegistration sampleAdminRegistration = SampleAdminContext.addServlet("ServletContainer", new ServletContainer(sampleAdminRc));
sampleAdminRegistration.addMapping("/*");
/**
* Deploy Server
*/
SampleAdminContext.deploy(httpServer);
httpServer.start();
System.out.println("Jersey app started with WADL available at:");
System.out.println(BASE_URI + "SampleAdmin/application.wdl");
System.out.println("Hit enter to stop it...");
System.in.read();
httpServer.shutdown();
}
catch(Exception ex) {
System.err.println("Error: " + ex.getMessage());
}
}
来源:https://stackoverflow.com/questions/45150746/grizzly-static-content-path-below-the-context-webapp