Jersey 2.26 - 启动服务器

时间:2018-02-15 08:33:47

标签: java jersey-2.0

我正在尝试使用Jersey和Jetty设置一个简单的服务器。但是Jersey documentation的代码会引发此错误:

Exception in thread "main" java.lang.NoSuchFieldError: GIT_HASH
at org.eclipse.jetty.server.Server.doStart(Server.java:370)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
at org.glassfish.jersey.jetty.JettyHttpContainerFactory.createServer(JettyHttpContainerFactory.java:305)
at org.glassfish.jersey.jetty.JettyHttpContainerFactory.createServer(JettyHttpContainerFactory.java:135)
at com.zoho.dfs.App.main(App.java:17)

这是我的App.java课程:

public class App
{
    public static void main(String[] args) {

        URI baseUri = UriBuilder.fromUri("http://127.0.0.1/").port(8050).build();
        ResourceConfig config = new ResourceConfig(RestController.class);
        Server server = JettyHttpContainerFactory.createServer(baseUri, config);

        try {
            server.start();
            server.join();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

RestController.class(我想要映射

@Path("/{username}")
public class RestController {

    @GET
    @Path("/home")
    @Produces(MediaType.APPLICATION_JSON)
    public Response read(@PathParam("username") String username) {
        String name = "hello " + username;

        return Response.status(200).entity(name).build();
    }
}

这里是pom.xml依赖项:

<dependencies>
    <dependency>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-server</artifactId>
        <version>9.4.8.v20171121</version>
    </dependency>
    <dependency>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-servlet</artifactId>
        <version>9.4.8.v20171121</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-jetty-http</artifactId>
        <version>2.26</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-moxy</artifactId>
        <version>2.26</version>
    </dependency>
    <dependency>
        <groupId>org.json</groupId>
        <artifactId>json</artifactId>
        <version>20180130</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.inject</groupId>
        <artifactId>jersey-hk2</artifactId>
        <version>2.26</version>
    </dependency>
</dependencies>

我尝试添加jersey-serverjersey-container-servlet-core等依赖项。这是同样的错误。我在做什么可能是错误的?

1 个答案:

答案 0 :(得分:2)

从您的依赖项中删除jetty-serverjetty-servlet,它们将由您需要添加的jersey-container-jetty-httpjersey-container-jetty-servlet提取。以下是您需要集成jettyjersey所需的一些基本依赖项:

<dependencies>
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-jetty-http</artifactId>
        <version>2.26</version>
    </dependency>

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

    <dependency>
        <groupId>org.glassfish.jersey.inject</groupId>
        <artifactId>jersey-hk2</artifactId>
        <version>2.26</version>
    </dependency>

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

    <dependency>
        <groupId>org.json</groupId>
        <artifactId>json</artifactId>
        <version>20180130</version>
    </dependency>
</dependencies>

此外,在App.java中,您无需执行server.start()

public class App
{
    public static void main(String[] args) {

        URI baseUri = UriBuilder.fromUri("http://127.0.0.1/").port(8050).build();
        ResourceConfig config = new ResourceConfig(RestController.class);
        Server server = JettyHttpContainerFactory.createServer(baseUri, config);

    }
}

执行JettyHttpContainerFactory.createServer(baseUri, config)时,它会创建一个具有给定config的容器,然后创建一个服务器并启动它。