【问题标题】:Jersey returning 404, after invoking the correct @PathJersey 在调用正确的@Path 后返回 404
【发布时间】:2013-11-11 10:54:26
【问题描述】:

这件事让我困惑了很长一段时间.. 我正在尝试在 Jetty 中嵌入一个 Jersey 容器。按照示例,我可以嵌入 Jersey,并且我看到我暴露的 REST 方法正在暴露。但是,由于某些奇怪的原因,我的 Jersey 确实返回了 404。看起来 Jersey 无法解析正确的端点。不幸的是,它没有告诉我原因,只是抛出了 NotFoundException :-(.

我 100% 确定我的方法被调用。该方法中的 System.out.println 显示在控制台中,并且我的 Eclipse 调试器清楚地通过了设置的断点。我是否使用 Jersey 2.0、2.3、2.4、1、2.4 都没关系。都有相同的结果..这里是我配置的代码sn-ps:

StartJetty.java:

        final int mainport = 9123;

        Server jettyServer = new Server(mainport);
        HandlerCollection handlerCollection = new ContextHandlerCollection();
        jettyServer.setHandler(handlerCollection);

        ServletHolder jerseyServlet = context.addServlet(org.glassfish.jersey.servlet.ServletContainer.class, "/");
        jerseyServlet.setInitOrder(1);
        jerseyServlet.setInitParameter("jersey.config.server.provider.packages", "com.test.rest");

        handlerCollection.addHandler(context);
    jettyServer.start();
        jettyServer.join();

来自 REST 资源的片段: com.test.rest.PersonsRSImpl.class:

@Path("/persons")
@Produces( MediaType.APPLICATION_JSON)
public class PersonsRSImpl {

    private PersonManager personMgr = PersonManager.getInstance();


    @Path("/list")
    public Collection<Person> list() {
        System.out.println("In Person::list" );
        return personMgr.getPersons();
    }


}

My Person 对象是一个简单的 POJO,带有一些 JAXB 注释。添加/删除它们根本没有区别。

最后但同样重要的是: pom.xml http://maven.apache.org/maven-v4_0_0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <groupId>com.test</groupId>
    <artifactId>integrationexample</artifactId>
    <packaging>jar</packaging>
    <version>1.0.0-SNAPSHOT</version>
    <name>Test project</name>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.build.outputEncoding>UTF-8</project.build.outputEncoding>
        <jetty.version>7.5.4.v20111024</jetty.version>
        <jersey.version>2.4.1</jersey.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-server</artifactId>
            <version>${jetty.version}</version>
        </dependency>
        <dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-servlet</artifactId>
            <version>${jetty.version}</version>
        </dependency>

        <dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-http-spi</artifactId>
            <version>${jetty.version}</version>
        </dependency>

        <!-- Jersey JAX-RS -->
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet</artifactId>
            <version>${jersey.version}</version>
        </dependency>

        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-simple-http</artifactId>
            <version>${jersey.version}</version>
        </dependency>

        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-moxy</artifactId>
            <version>${jersey.version}</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
    </dependencies>


    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>com.test.StartJetty</mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

我启用了 Jersey 的踪迹(谢谢 Michal)。结果跟踪是:

球衣痕迹

HTTP/1.1 404 Not Found
X-Jersey-Tracing-000: START       [ ---- /  ---- ms |  ---- %] baseUri=[http://localhost:9123/rest/] requestUri=[http://localhost:9123/rest/persons/list] method=[GET] authScheme=[n/a] accept=[text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8] accept-encoding=[gzip, deflate] accept-charset=n/a accept-language=[en-US,en;q=0.5] content-type=n/a content-length=n/a
X-Jersey-Tracing-001: PRE-MATCH   [ 0,01 /  1,03 ms |  0,10 %] PreMatchRequest summary: 0 filters
X-Jersey-Tracing-002: MATCH       [ ---- /  1,21 ms |  ---- %] Matching path [/persons/list]
X-Jersey-Tracing-003: MATCH       [ ---- /  1,27 ms |  ---- %] Pattern [/persons(/.*)?] IS selected
X-Jersey-Tracing-004: MATCH       [ ---- /  1,41 ms |  ---- %] Matched resource: template=[/persons] regexp=[/persons(/.*)?] matches=[/persons] from=[/persons/list]
X-Jersey-Tracing-005: MATCH       [ ---- /  1,54 ms |  ---- %] Matching path [/list]
X-Jersey-Tracing-006: MATCH       [ ---- /  1,58 ms |  ---- %] Pattern [/list(/.*)?] IS selected
X-Jersey-Tracing-007: MATCH       [ ---- /  1,76 ms |  ---- %] Matched resource: template=[/list] regexp=[/list(/.*)?] matches=[/list] from=[/list]
X-Jersey-Tracing-008: MATCH       [ ---- /  1,90 ms |  ---- %] Matched locator : public java.util.Collection com.test.rest.PersonsRSImpl.list()
X-Jersey-Tracing-009: MATCH       [ ---- /  2,08 ms |  ---- %] Resource instance: [com.test.rest.PersonsRSImpl @3cceafcb]
X-Jersey-Tracing-010: MATCH       [ ---- /  2,21 ms |  ---- %] Resource instance: [java.util.HashMap$Values @25591d82]
X-Jersey-Tracing-011: MATCH       [ 4,23 /  5,30 ms | 75,92 %] RequestMatching summary
X-Jersey-Tracing-012: RESP-FILTER [ 0,00 /  5,52 ms |  0,05 %] Response summary: 0 filters
X-Jersey-Tracing-013: FINISHED    [ ---- /  5,58 ms |  ---- %] Response status: 404/CLIENT_ERROR|Not Found
Cache-Control: must-revalidate,no-cache,no-store
Content-Type: text/html;charset=ISO-8859-1
Content-Length: 1284
Server: Jetty(7.5.4.v20111024)

希望你们中的一些人知道发生了什么!

【问题讨论】:

  • 能不能打开tracing support看看方法调用后的情况?将值为ALL 的jersey.config.server.tracing servlet init-param 添加到您的servlet 配置中就足够了。
  • 感谢 Michal 的提示。我添加了跟踪支持,请参阅上面的原始帖子。
  • 无关的咆哮,但是:Content-Type: text/html;charset=ISO-8859-1... Noo!请停止使用 ISO-8859-1! :(
  • 无法更新 cmets,因此单独发布:现在文档已移至 eclipse 空间,请参阅 tracing support docs here

标签: java rest maven jersey jetty


【解决方案1】:

您需要使用注释来注释您的资源方法list(),该注释指示资源方法应响应哪个HTTP方法(在本例中为@GET):

@GET
@Path("/list")
public Collection<Person> list() {
    System.out.println("In Person::list" );
    return personMgr.getPersons();
}

否则资源方法将被调用,但它会被视为子资源定位器。有关子资源和子资源定位器的更多信息,请参阅JAX-RS 2.0 spec 或专门针对Sub-resources 的 Jersey 用户指南。

【讨论】:

  • 谢谢,确实解决了问题!我完全忽略了那部分..也感谢您对子资源的解释。
猜你喜欢
  • 2018-02-06
  • 2018-12-13
  • 1970-01-01
  • 2018-10-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-25
相关资源
最近更新 更多