【问题标题】:Jersey gives 404 - Not Found on valid serviceJersey 给出 404 - Not Found on valid service
【发布时间】:2013-11-11 17:36:51
【问题描述】:

我的 Jersey Rest Servlet 遇到一个奇怪的问题,我使用 Jersey 1.17.1 和下面的配置,但是,当我尝试访问我的 rest 服务时,它只给了我一个 404 - Not Found 错误。

奇怪的是这个日志:accept root resource classes: "/", but I'm not request on "/" 我在请求 " /r/..."

com.sun.jersey.config.property.resourceConfigClass

并且日志显示服务加载正确:

Root resource classes found:
  class com.xxx.restful.SearchRESTService

在这里你可以看到服务正在运行:

2 < X-Jersey-Trace-001: match path "/" -> "/application\.wadl(/.*)?", "/request(/.*)?", "/search(/.*)?"

web.xml

<context-param>
    <param-name>context-root</param-name>
    <param-value>root</param-value>
</context-param>

<servlet>
        <servlet-name>JerseyREST</servlet-name>
        <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>com.sun.jersey.config.property.resourceConfigClass</param-name>
            <param-value>com.xxx.restful.ResourceConfig</param-value>
        </init-param>
        <init-param>
            <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
            <param-value>true</param-value>
        </init-param>

        <init-param>
            <param-name>com.sun.jersey.spi.container.ContainerRequestFilters</param-name>
            <param-value>com.sun.jersey.api.container.filter.LoggingFilter</param-value>
        </init-param>
        <init-param>
            <param-name>com.sun.jersey.spi.container.ContainerResponseFilters</param-name>
            <param-value>com.sun.jersey.api.container.filter.LoggingFilter</param-value>
        </init-param>
        <init-param>
            <param-name>com.sun.jersey.config.feature.Trace</param-name>
            <param-value>true</param-value>
        </init-param>
        <init-param>
            <param-name>com.sun.jersey.config.feature.Debug</param-name>
            <param-value>true</param-value>
        </init-param>

        <load-on-startup>3</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>JerseyREST</servlet-name>
        <url-pattern>/r/*</url-pattern>
    </servlet-mapping>

休息服务:

@Path("/search")
public class SearchRESTService {

    @GET
    @Path("/related")
    @Produces({ MediaType.APPLICATION_JSON })
    public Response getRelatedSearch(@QueryParam("query") String query) {
        List<String> result = ...;
        return Response.ok(result).build();
    }
}

启动日志

INFO: Initiating Jersey application, version 'Jersey: 1.17.1 02/28/2013 12:47 PM' Oct 31, 2013 1:37:52 PM com.sun.jersey.api.core.PackagesResourceConfig init
INFO: Scanning for root resource and provider classes in the packages:
  com.xxx.restful
Oct 31, 2013 1:37:52 PM com.sun.jersey.api.core.ScanningResourceConfig logClasses
INFO: Root resource classes found:
  class com.xxx.restful.SearchRESTService
Oct 31, 2013 1:37:52 PM com.sun.jersey.api.core.ScanningResourceConfig logClasses
INFO: Provider classes found:
  class com.xxx.restful.NucleusProducer
Oct 31, 2013 1:37:52 PM com.sun.jersey.server.impl.application.DeferredResourceConfig$ApplicationHolder <init>
INFO: Instantiated the Application class com.xxx.restful.NucleusResourceConfig

请求日志

Oct 31, 2013 1:38:08 PM com.sun.jersey.api.container.filter.LoggingFilter filter
INFO: 2 * Server in-bound request
2 > OPTIONS http://localhost:7103/root/r/search/related?query=arduino
2 > Host: localhost:7103
2 > Connection: keep-alive
2 > User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36
2 > Origin: chrome-extension://hgmloofddffdnphfgcellkdfbfbjeloo
2 > Content-Type: application/json
2 > Accept: */*
2 > DNT: 1
2 > Accept-Encoding: gzip,deflate,sdch
2 > Accept-Language: en-US,en;q=0.8,pt-BR;q=0.6,pt;q=0.4
2 >

Oct 31, 2013 1:38:08 PM com.sun.jersey.api.container.filter.LoggingFilter$Adapter finish
INFO: 2 * Server out-bound response
2 < 404
2 < X-Jersey-Trace-000: accept root resource classes: "/"
2 < X-Jersey-Trace-001: match path "/" -> "/application\.wadl(/.*)?", "/request(/.*)?", "/search(/.*)?"
2 < X-Jersey-Trace-002: mapped exception to response: com.sun.jersey.api.NotFoundException@7fae8378 -> 404 (Not Found)
2 <

【问题讨论】:

  • 你的服务器支持context-root上下文参数吗?如果您的网址中不使用root,它会说明什么?

标签: rest servlets jersey http-status-code-404 jax-rs


【解决方案1】:

我认为您需要在 web.xml 中添加以下行。假设你的 Jersey 类在包 com.example 中,那么你需要添加:

<init-param>
   <param-name>com.sun.jersey.config.property.packages</param-name>
   <param-value>com.example</param-value>
</init-param>

这将确保 Jersey 可以找到您的根资源类。

【讨论】:

  • 这不是问题,因为服务加载了 com.sun.jersey.config.property.resourceConfigClass 属性。并且日志显示服务加载正确: Root resource classes found: class com.xxx.restful.SearchRESTService 在这里可以看到服务正在运行: 2 “/application\.wadl(/.*)?”、“/request(/.*)?”、“/search(/.*)?”
  • 如果将/r/* 更改为/* 会怎样?您是否看到相同的 404 错误?
  • 是的,同样的错误。我将服务从 /search 更改为 / 并且它们给出了 403,而不是允许的方法。
  • 您能找出问题所在吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-12-18
  • 2013-03-10
  • 2012-12-22
  • 2014-09-29
  • 2021-07-17
  • 1970-01-01
  • 2018-01-04
相关资源
最近更新 更多