3

我有一个非常简单的 Java REST 应用程序,它使用 grizzly2 (jersey-grizzly2 1.12) 和 jersey (jersey-bundle 1.12)

当我发出第一个 http 请求时,我得到一个 404 响应(但映射的方法仍然在服务器上执行)。如果我再次发出相同的请求,那么我会得到预期的 200 响应。

疯狂的部分是,即使是 404 错误,映射的方法仍然执行。

也许有一个错误?还是我错误地配置了我的服务器?这是我第一次尝试 grizzly2,所以欢迎任何见解!

我这样启动服务器:

final ResourceConfig rc = new DefaultResourceConfig(
            TestResource.class
            );
rc.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, true);
rc.getFeatures().put(ResourceConfig.FEATURE_TRACE, true);

URI path = UriBuilder.fromPath(baseUri).build();                        
HttpServer httpServer = GrizzlyServerFactory.createHttpServer(path, rc);            
httpServer.start(); 

我的资源如下所示:

@Path("/test")
public class TestResource {
    @GET    
    @Produces(MediaType.APPLICATION_JSON)
    @Path("go") 
    public Integer startSomethingElse() {
        return new Integer(4);
    }        
}

还有我的示例连接示例:

C:\Users\Paul>curl -v -H "Accept: application/json"  http://10.0.0.156:9998/test/go
* About to connect() to 10.0.0.156 port 9998 (#0)
*   Trying 10.0.0.156... connected
> GET /test/go HTTP/1.1
> User-Agent: curl/7.23.1 (x86_64-pc-win32) libcurl/7.23.1 OpenSSL/0.9.8r zlib/1.2.5
> Host: 10.0.0.156:9998
> Accept: application/json
>
< HTTP/1.1 404 Not Found
< Content-Type: text/html
< Date: Tue, 12 Jun 2012 17:49:24 GMT
< Content-Length: 1061
<
<html><head><title>Grizzly/2.1</title><style><!--div.header {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#003300;font-size:22px;-moz-border-radius-topleft: 10px;border-top-left-radius: 10px;-moz-border-radius-topright: 10px;border-top-right-radius: 10px;padding-left: 5px}div.body {font-family:Tahoma,Arial,sans-serif;color:black;background-color:#FFFFCC;font-size:16px;padding-top:10px;padding-bottom:10px;padding-left:10px}div.footer {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#666633;font-size:14px;-moz-border-radius-bottomleft: 10px;border-bottom-left-radius: 10px;-moz-border-radius-bottomright: 10px;border-bottom-right-radius: 10px;padding-left: 5px}BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;}B {font-family:Tahoma,Arial,sans-serif;color:black;}A {color : black;}HR {color : #999966;}--></style> </head><body><div class="header">Not Found</div><div class="body">Resource identified by path '/test/go', does not exist.</div><div class="footer">Grizzly/2.1</div></body></html>* Connection #0 to host 10.0.0.156 left intact
Closing connection #0

然后第二次尝试(完全相同的请求,紧随第一次之后)

C:\Users\Paul>curl -v -H "Accept: application/json"  http://10.0.0.156:9998/test/go
About to connect() to 10.0.0.156 port 9998 (#0)
Trying 10.0.0.156... connected
> GET /test/go HTTP/1.1
> User-Agent: curl/7.23.1 (x86_64-pc-win32) libcurl/7.23.1 OpenSSL/0.9.8r zlib/1.2.5
> Host: 10.0.0.156:9998
> Accept: application/json
>
< HTTP/1.1 200 OK
< Content-Type: application/json
< X-Jersey-Trace-000: accept root resource classes: "/test/go"
< X-Jersey-Trace-001: match path "/test/go" -> "/application\.wadl(/.*)?", "/test(/.*)?"
< X-Jersey-Trace-002: accept right hand path java.util.regex.Matcher[pattern=/test(/.*)? region=0,8 lastmatch=/test/go]: "/test/go" -> "/test" : "/go"
< X-Jersey-Trace-003: accept resource: "test" -> @Path("/test") com.subserveo.skyhook.http.resource.TestResource@30813486
< X-Jersey-Trace-004: match path "/go" -> "/go2(/)?", "/go(/)?", ""
< X-Jersey-Trace-005: accept right hand path java.util.regex.Matcher[pattern=/go(/)? region=0,3 lastmatch=/go]: "/go" -> "/go" : ""
< X-Jersey-Trace-006: accept sub-resource methods: "test" : "/go", GET -> com.subserveo.skyhook.http.resource.TestResource@30813486
< X-Jersey-Trace-007: matched sub-resource method: @Path("/go") public java.lang.Integer com.subserveo.skyhook.http.resource.TestResource.startSomethingElse()
< X-Jersey-Trace-008: matched message body writer: java.lang.Integer@4, "application/json" -> com.sun.jersey.json.impl.provider.entity.JacksonProviderProxy@576f8789
< Date: Tue, 12 Jun 2012 17:49:26 GMT
< Transfer-Encoding: chunked
<
4* Connection #0 to host 10.0.0.156 left intact
* Closing connection #0
4

0 回答 0