具有RESTFul API和静态Web的独立Java应用程序

时间:2014-07-06 10:33:12

标签: java maven jersey jax-rs grizzly

我的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不起作用,但静态网络部署到&#34; /&#34;工作中。 Please check this screen.

然后当我更改几行代码时:

server.getServerConfiguration().addHttpHandler(new StaticHttpHandler("src/main/resources/web"),"/www");

API和静态网络正在运行,但网络不在&#34; /&#34;它在&#34; / www&#34;。 Please check this.

请帮助我在&#34; / api&#34;和静态网站&#34; /&#34; ?

2 个答案:

答案 0 :(得分:1)

创建像baseuri一样的基础:

 final URI baseUri = UriBuilder.fromUri("http://localhost/api").port(9998).build();

答案 1 :(得分:0)

你可以通过包含静态处理程序管理哪些扩展来解决这个问题,例如/*.jpg,* .html,...

这样服务器应该能够选择适当的处理程序。