在我的 pom.xml 中,我添加了一个依赖项。
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-grizzly2-http</artifactId>
<version>2.10.1</version>
</dependency>
代码主要部分:
public static void main(String[] args) {
final URI baseUri = UriBuilder.fromUri("http://localhost").port(9998).build();
//add test configs
Set rest = new HashSet<Class>();
rest.add(TestResource.class);
final ResourceConfig config = new ResourceConfig(rest);
HttpServer server = GrizzlyHttpServerFactory.createHttpServer(baseUri, config);
try {
//server.getServerConfiguration().addHttpHandler(new StaticHttpHandler("web/"),"/w");
server.getServerConfiguration().addHttpHandler(new StaticHttpHandler("src/main/resources/web"),"/");
server.start();
System.out.println();
System.out.println("Press enter to stop the server...");
System.in.read();
} catch (IOException e) {
e.printStackTrace();
} finally {
server.shutdownNow();
}
}
在这种情况下,API 不起作用,但部署到“/”的静态 Web 正在工作。 Please check this screen.
然后当我更改几行代码时:
server.getServerConfiguration().addHttpHandler(new StaticHttpHandler("src/main/resources/web"),"/www");
API 和静态网络正在工作,但网络不在“/”,而在“/www”。 Please check this.
您能帮我在“/api”获取工作 API 并在“/”获取静态 Web 吗?
最佳答案
创建baseuri,例如:
final URI baseUri = UriBuilder.fromUri("http://localhost/api").port(9998).build();
关于java - 具有 RESTFul API 和静态 Web 的独立 Java 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24594919/