与Grizzly一起运行泽西岛

时间:2015-02-10 18:46:16

标签: java jersey-2.0 grizzly

我正在尝试将泽西与Grizzly一起用作自托管

基本上我的主要是:

org.glassfish.jersey.server.ResourceConfig rc = new ResourceConfig();
rc.registerClasses(DummyController.class);
webServer = GrizzlyHttpServerFactory.createHttpServer(uri, rc, false);
System.out.println("Server Created");
try {
    webServer.start();
    System.out.println("Server Started");
} catch (IOException e) {
}

我创建了一个小型控制器:

@Path("/")
public class DummyController {
    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String get() {
        return "Got it!";
    }
}

然而,当我运行它时,我得到一个例外:

Exception in thread "main" java.lang.NoSuchMethodError: javax.ws.rs.core.Application.getProperties()Ljava/util/Map;
at org.glassfish.jersey.server.ApplicationHandler.<init>(ApplicationHandler.java:309)
at org.glassfish.jersey.server.ApplicationHandler.<init>(ApplicationHandler.java:289)
at org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpContainer.<init>(GrizzlyHttpContainer.java:331)
at org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory.createHttpServer(GrizzlyHttpServerFactory.java:141)
at RestServer.Server.main(Server.java:35)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)

当我没有添加资源时,灰熊服务器运行正常,但我无法访问任何控制器

我使用Maven下载所有依赖项

这是pom文件

<?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>TestRestServer</groupId>
<artifactId>TestRestServer</artifactId>
<version>1.0-SNAPSHOT</version>

<dependencies>
    <dependency>
        <groupId>org.glassfish.grizzly</groupId>
        <artifactId>grizzly-framework</artifactId>
        <version>2.3.18</version>
    </dependency>
    <dependency>
        <groupId>javax.annotation</groupId>
        <artifactId>javax.annotation-api</artifactId>
        <version>1.2</version>
    </dependency>
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-grizzly2</artifactId>
        <version>1.18.3</version>
    </dependency>
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-json</artifactId>
        <version>1.18.3</version>
    </dependency>

    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-grizzly2-servlet</artifactId>
        <version>2.15</version>
    </dependency>
</dependencies>

1 个答案:

答案 0 :(得分:5)

解决方案是将com.sun.jersey中的依赖关系从org.glassfish.jersey更改为pom.xml。第一部分包含JAX-RS 1.x API(Java EE 6的一部分)的实现,后者包含JAX-RS 2.0 API实现(Java EE 7的一部分)。