簡體   English   中英

舊項目上的 Jetty 和 Jersey 404 錯誤

[英]Jetty and Jersey 404 error on an old project

作為我工作的一部分,我正在嘗試更新我們的一些服務,其中一項服務正在與 Jetty & Jersey 一起部署。 不幸的是,代碼很舊(一些依賴項來自 2014 年),並且在編譯和服務器啟動時,每當我從我的 IDE 中使用mvn jetty:run ,根目錄都會正確顯示Directory /消息,而對於所有其他 URL,我得到錯誤404 NOT FOUND

我在控制台上沒有收到任何警告/錯誤消息,我也找不到它發生的原因。 我搜索了很多,但代碼似乎是正確的,唯一缺少的是web.xml但我認為 Jetty 是嵌入的,我需要提到的是,這段代碼已經作為 JAR 在生產中運行。

您可以在下面看到一些文件:

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.sample</groupId>
    <artifactId>sample</artifactId>
    <version>2.0</version>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.eclipse.jetty</groupId>
                <artifactId>jetty-maven-plugin</artifactId>
                <version>9.2.2.v20140723</version>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.21</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.21</version>
        </dependency>

        <dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-server</artifactId>
            <version>9.2.3.v20140905</version>
        </dependency>
        <dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-servlet</artifactId>
            <version>9.2.3.v20140905</version>
        </dependency>

        <dependency>
            <groupId>org.glassfish.jersey.core</groupId>
            <artifactId>jersey-server</artifactId>
            <version>2.7</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet-core</artifactId>
            <version>2.7</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-jetty-http</artifactId>
            <version>2.7</version>
        </dependency>

        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet</artifactId>
            <version>2.7</version>
        </dependency>

        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-moxy</artifactId>
            <version>2.7</version>
        </dependency>

        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20160212</version>
        </dependency>

    </dependencies>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

</project>

引用下面的類來啟動服務器:
ApiService.java


import com.simple.api.restendpoint.*;
import org.apache.log4j.Logger;
import org.eclipse.jetty.http.HttpVersion;
import org.eclipse.jetty.server.*;
import org.eclipse.jetty.server.handler.IPAccessHandler;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.util.ssl.SslContextFactory;
import org.eclipse.jetty.util.thread.QueuedThreadPool;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.servlet.ServletContainer;

import java.util.HashSet;
import java.util.Set;


public class ApiService {
    Logger log = Logger.getLogger(ApiService.class);
    
    private static ApiService instance;
    private final Server server;

    private ApiService(){
        //CREATE CONFIG
        Set<Class<?>> s = new HashSet<>();
        s.add(restEndpoint.class);      

        ResourceConfig config = new ResourceConfig(s);

        //CREATE CONTAINER
        ServletContainer container = new ServletContainer(config);

        //CREATE CONTEXT
        ServletContextHandler context = new ServletContextHandler();
        context.setContextPath("/");
        context.addServlet(new ServletHolder(container),"/*");

        //CREATE RPC SERVER
        QueuedThreadPool threadPool = new QueuedThreadPool();
        threadPool.setMaxThreads(50);
        server = new Server(threadPool);

        server.setHandler(context);

        ServerConnector connector = new ServerConnector(server);
        connector.setPort(8080);

        server.addConnector(connector);

        SslContextFactory sslContextFactory = new SslContextFactory();
        sslContextFactory.setKeyStorePath("testkeystore");
        sslContextFactory.setKeyStorePassword("testkeystore");
        sslContextFactory.setKeyManagerPassword("testkeystore");

        // SSL HTTP Configuration
        HttpConfiguration https_config = new HttpConfiguration();
        https_config.addCustomizer(new SecureRequestCustomizer());

        // SSL Connector
        ServerConnector sslConnector = new ServerConnector(server,
            new SslConnectionFactory(sslContextFactory,HttpVersion.HTTP_1_1.asString()),
            new HttpConnectionFactory(https_config));
        sslConnector.setPort(Settings.getInstance().getRpcPort());
        server.addConnector(sslConnector);
        
    }

    public static ApiService getInstance() {
        if (instance==null) {
            instance = new ApiService();
        }
        return instance;
    }

    public void start() {
        try {
            //START RPC 
            server.start();
            server.join();
            log.info("Server started");
        }
        catch (Exception e) {
            //FAILED TO START RPC
            log.error("Error starting server");
        } finally {
            server.destroy();
        }
    }
}

restEndpoint.java

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.container.AsyncResponse;
import javax.ws.rs.container.Suspended;
import javax.ws.rs.core.Context;
import org.apache.log4j.Logger;
import org.json.JSONArray;
import org.json.JSONObject;

@Path("/v4/restEndpoint")
@Produces({"application/json"})
public class restEndpoint {
    Logger log = Logger.getLogger(restEndpoint.class);

    private static final String VERSION = "v4";

    private static final ExecutorService executors = Executors.newFixedThreadPool(20);

    @Context
    HttpServletRequest request;

    @GET
    @Path("/getsamplejson")
    public void getSampleJson(@Suspended final AsyncResponse asyncResponse) {
        executors.execute(new Runnable() {
            @Override
            public void run() {
                String result = veryExpensiveOperation();
                asyncResponse.resume(result);
            }

            private String veryExpensiveOperation() {
                JSONObject sampleJson = new JSONObject().put("test", 0);
                return sampleJson.toString();
            }
        });
    }
}

知道為什么會這樣嗎? http://localhost:8080/v4/restEndpoint/getsamplejson返回404 NOT FOUND http://localhost:8080/顯示Directory: /

我應該嘗試將它構建為 JAR 並通過它運行它,而不是使用mvn jetty:run命令嗎?

您有一個從代碼開始的嵌入式碼頭實例。

您提供的ApiService源證明了這ApiService

您不能使用jetty-maven-plugin來管理/啟動/運行該類。

jetty-maven-plugin專門用於處理 webapps,無論是作為 WAR 打包文件,還是作為擴展的 webapp 目錄。 (webapps 將在其中包含 WEB-INF/web.xml 和/或 Servlet 注釋類)

您需要使用項目獨有的其他方式啟動服務器,可能是某種主類或測試用例,最終實例化ApiService並在其上調用ApiService .start()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM