【问题标题】:Standalone Java application with RESTFull API and static web具有 RESTFul API 和静态 Web 的独立 Java 应用程序
【发布时间】:2014-07-06 10:33:11
【问题描述】:

在我的 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,在“/”获得静态网络吗?

【问题讨论】:

    标签: java maven jersey jax-rs grizzly


    【解决方案1】:

    像这样创建baseuri:

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

    【讨论】:

    • 谢谢。但没有帮助:(然后一样。
    • 嗯,在这个改变之后它对我有用。你到底想达到什么目的? :)
    【解决方案2】:

    您可以通过使用 /*.jpg、*.html、...等模式包含您希望由静态处理程序管理哪些扩展来解决此问题。

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

    【讨论】:

    • 你能发布你的新配置吗?
    • server.getServerConfiguration().addHttpHandler(new StaticHttpHandler("src/main/resources/web/*"),"/www");
    • 或者喜欢这个 server.getServerConfiguration().addHttpHandler(new StaticHttpHandler("src/main/resources/web/*.html"),"/www");
    • 我会尝试这样的 StaticHttpHandler("src/main/resources/web/"),"/*.html");
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-21
    • 1970-01-01
    • 1970-01-01
    • 2019-06-14
    • 2011-07-22
    • 1970-01-01
    相关资源
    最近更新 更多