【问题标题】:Embedded Jersey 3 with Jetty into existing Application将带有 Jetty 的 Jersey 3 嵌入到现有应用程序中
【发布时间】:2022-07-08 01:24:55
【问题描述】:

我正在尝试将 HTTP-Server 嵌入到现有的 java 应用程序中。我的目标是创建一个小的 rest API 作为接口,将命令发送到它运行的服务器应用程序。

我计划使用带有 Jetty 的 Jakarta 和 Jersey 3 作为嵌入式 HTTP-Server。我的出发点是针对 Jersey 2 的以下主题,但我试试运气:Embed jersey in java application

我的问题是,当我尝试在浏览器中调用 http://localhost/login/status 时,我得到了 404 Not Found。页面是空白的。当我切换到使用 Grizzly2 作为嵌入式 HTTP 服务器并在浏览器中键入 url 时,结果是相同的。我能发现的 Grizzly2 的唯一区别是,当我只调用 http://localhost/ 时,除了404 Not Found 响应返回时,我还会收到一个错误页面。一旦我将 /login 添加到 url,我就会得到 404 Not Found 响应,而没有错误页面。服务器不接收我的资源的原因可能是什么?

我正在使用 Eclipse IDE。首先,我创建了一个干净的 Maven 项目,添加了以下依赖项并创建了我的测试代码:

org.glassfish.jersey.core -> jersey-server
org.glassfish.jersey.containers -> jersey-container-jetty-http

在我第一次启动时,我遇到了一些缺少的类错误,搜索了它们所在的依赖项并将它们添加到 pom.xml 中。以下是我目前的测试代码。

<!-- pom.xml -->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>test-ee</groupId>
    <artifactId>test-ee-embed</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.glassfish.jersey</groupId>
                <artifactId>jersey-bom</artifactId>
                <version>3.0.4</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.glassfish.jaxb</groupId>
                <artifactId>jaxb-bom</artifactId>
                <version>3.0.2</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>jakarta.servlet</groupId>
            <artifactId>jakarta.servlet-api</artifactId>
            <version>5.0.0</version>
        </dependency>
        <dependency>
            <groupId>jakarta.xml.bind</groupId>
            <artifactId>jakarta.xml.bind-api</artifactId>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.core</groupId>
            <artifactId>jersey-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.inject</groupId>
            <artifactId>jersey-hk2</artifactId>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jaxb</groupId>
            <artifactId>jaxb-runtime</artifactId>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet</artifactId>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-jetty-http</artifactId>
        </dependency>
    </dependencies>
</project>
// LoginserverRestApi.java
package de.l2d;

import org.glassfish.jersey.server.ResourceConfig;

import jakarta.ws.rs.ApplicationPath;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;

@ApplicationPath("/login")
public class LoginserverRestApi extends ResourceConfig {

    @GET @Path("/status")
    public String status() {
        // TODO Return real server statistics.
        return "{\"status\":\"ok\"}";
    }

}
// RestApiServer.java
package de.l2d;

import java.net.URI;

import org.eclipse.jetty.server.Server;
import org.glassfish.jersey.jetty.JettyHttpContainerFactory;

import jakarta.ws.rs.core.UriBuilder;

public class RestApiServer {
    private Server server;

    private RestApiServer() {
        URI baseUri = UriBuilder.fromUri("http://localhost/").build();
        server = JettyHttpContainerFactory.createServer(baseUri, new LoginserverRestApi(), false);
    }
    
    public void start() throws Exception {
        server.start();
    }
    
    public void stop() throws Exception {
        server.stop();
    }

    private static final class SingletonHolder {
        protected static RestApiServer instance = new RestApiServer();
    }
    public static RestApiServer getInstance() {
        return SingletonHolder.instance;
    }
}
// Main.java
package de.l2d;

public class Main {
    public static void main(String[] args) throws Exception {
        RestApiServer.getInstance().start();
        Thread.currentThread().join();
        RestApiServer.getInstance().stop();
    }
}

【问题讨论】:

  • 你没有放一个端口。不知道是不是这个原因。

标签: java maven jersey jetty embedded-jetty


【解决方案1】:

我找到了解决问题的方法。但是,我不知道它为什么会这样。

我必须在LoginserverRestApi 类上使用Path 注释而不是ApplicationPath 注释,并另外构造一个ResourceConfig,以LoginserverRestApi.class 作为参数并将该对象传递给JettyHttpContainerFactory.createServer 静态方法。以下是与初始帖子不同的两个文件:

// LoginserverRestApi.java
package de.l2d;

import org.glassfish.jersey.server.ResourceConfig;

import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;

@Path("/login")
public class LoginserverRestApi extends ResourceConfig {

    @GET @Path("/status")
    public String status() {
        // TODO Return real server statistics.
        return "{\"status\":\"ok\"}";
    }

}
// RestApiServer.java
package de.l2d;

import java.net.URI;

import org.eclipse.jetty.server.Server;
import org.glassfish.jersey.jetty.JettyHttpContainerFactory;
import org.glassfish.jersey.server.ResourceConfig;

import jakarta.ws.rs.core.UriBuilder;

public class RestApiServer {
    private Server server;

    private RestApiServer() {
        URI baseUri = UriBuilder.fromUri("http://localhost/").build();
        ResourceConfig config = new ResourceConfig(LoginserverRestApi.class);
        server = JettyHttpContainerFactory.createServer(baseUri, config, false);
    }
    
    public void start() throws Exception {
        server.start();
    }
    
    public void stop() throws Exception {
        server.stop();
    }

    private static final class SingletonHolder {
        protected static RestApiServer instance = new RestApiServer();
    }
    public static RestApiServer getInstance() {
        return SingletonHolder.instance;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-31
    • 2019-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多