嵌入式球衣3带JETTY进入现有应用

发布于 2025-01-20 07:50:30 字数 5295 浏览 4 评论 0原文

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

我计划使用Jakarta和Jersey 3和Jetty用Jetty作为嵌入式HTTP-Server。我的出发点是下面的主题,是针对球衣2的,但我尝试了运气:应用程序

我的问题是,当我尝试在浏览器中调用http:// localhost/login/status时,我找不到404。页面是空白的。当我切换为嵌入式HTTP-Server并在浏览器中键入URL时,结果是相同的。我可以发现的灰色2的唯一区别是,当我只致电http:// localhost/我还会在未找到的404响应回响应时,还会收到一个错误页面。一旦我添加 /登录到URL,就没有找到404 < /code>没有错误页面。服务器不拾取我的资源的原因是什么?

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

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

在我的第一个启动时,我遇到了一些类错误,搜索了它们所处的依赖项并将其添加到POM中。以下是我当前的测试代码。

<!-- 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();
    }
}

i am trying to embed a HTTP-Server into an existing java Application. My goal is to create a small rest API as an interface to send commands to the server application it runs in.

I planned using Jakarta and Jersey 3 with Jetty as embeded HTTP-Server. My starting point was the following topic which was for Jersey 2 but i tried my luck: Embed jersey in java application

My problem is that i get 404 Not Found back when i try to call http://localhost/login/status in my browser. The page is blank. When i switch to using Grizzly2 as embedded HTTP-Server and type the url in the browser, the result is the same. The only difference in Grizzly2 i could spot is when i only call http://localhost/ i get an error page additionally to the 404 Not Found response back. As soon as i add /login to the url, i get the 404 Not Found response without an error page. What could be the reason the server does not pick up my resources?

I am using the Eclipse IDE. First i created a clean Maven project, added the following dependencies and created my test code:

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

On my first startup i got some missing class errors, searched for the dependencies they are in and added them to the pom. Following is my current test code.

<!-- 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();
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟雨扶苏 2025-01-27 07:50:30

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

我必须在 LoginserverRestApi 类上使用 Path 注释而不是 ApplicationPath 注释,并另外构造一个 ResourceConfigLoginserverRestApi.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;
    }
}

I found the solution to my problem. However, i do not know why it behaves like this.

I had to use the Path annotation instead of the ApplicationPath annotation on the LoginserverRestApi class and additionally construct a ResourceConfig with LoginserverRestApi.class as parameter and pass that Object to the JettyHttpContainerFactory.createServer static method. Following are the two files which differ from the initial post:

// 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;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文