我的 Jersey Rest Servlet 遇到一个奇怪的问题,我使用 Jersey 1.17.1 和下面的配置,但是,当我尝试访问我的 rest 服务时,它只给我一个404 - Not Found错误。
奇怪的是这个日志:接受根资源类:“/”,但我不是在请求“ /”,而是在请求“/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 <