2015-11-06 93 views
1

我创建了一个简单GrizzlyHttpServerFactory.createHttpServer服务,并试图发出一个@ GET操作,其中,客户端将传递参数,但是我收到一个错误:GrizzlyHttpServerFactory.createHttpServer @GET与参数

enter image description here

我细读了几个例子,一切都很好。一切正常,只要我不尝试传递参数。这是我迄今为止的代码。

在主

()

final ResourceConfig resourceConfig = new ResourceConfig(TestServerResource.class); 
final HttpServer server = GrizzlyHttpServerFactory.createHttpServer(URI.create("http://localhost:8081/base/"), resourceConfig); 

在资源类:

@Path("TestServer") 
public class TestServerResource 
{ 
    public static final String CLICHED_MESSAGE = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n<note>\r\n<to>Tove</to>\r\n<from>Jani</from>\r\n<heading>Reminder</heading>\r\n<body>Don't forget me this weekend!</body>\r\n</note>\r\n\r\n"; 
    public static final String DO_MESSAGE = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n<DoTask>\r\n\t<Response>%s</Response>\r\n</DoTask>\r\n\r\n"; 

    @GET 
    @Path("getHello") 
    @Produces(MediaType.APPLICATION_XML) 
    public String getHello() 
    { 
     return CLICHED_MESSAGE; 
    } 

    @GET 
    @Path("testGet3") 
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED) 
    @Produces(MediaType.APPLICATION_XML) 
    public String testGet3(MultivaluedMap<String, String> formParams) 
    { 
     // Get the parameters. 
     String argTitle = formParams.getFirst("title"); 
     String argAuthor = formParams.getFirst("author"); 

     // Create the XML response. 
     String xmlResponse = String.format(DO_MESSAGE, String.format("Book Title %s with author %s", argTitle, argAuthor)); 

     return xmlResponse; 
    } 
} 

在我的CentOS 7机器上运行的服务之后。这里是终端命令。

作品(无参数):

[[email protected] Downloads]$ curl -H "Accept: application/xml" "http://localhost:8081/base/TestServer/getHello" 
<?xml version="1.0" encoding="UTF-8"?> 
<note> 
<to>Tove</to> 
<from>Jani</from> 
<heading>Reminder</heading> 
<body>Don't forget me this weekend!</body> 
</note> 

使用参数不工作:

[[email protected] Downloads]$ curl -H "Accept: application/xml" "http://localhost:8081/base/TestServer/testGet3?title=smurfs&author=Gargamel" 
<html><head><title>Grizzly 2.3.8</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">Request failed.</div><div class="body">Request failed.</div><div class="footer">Grizzly 2.3.8</div></body></html>[[email protected] Downloads]$ 

我做会将网址加上引号,传递参数的时候,所以这不是问题。思考?

这里仅仅是少数,我读了各种资源:

  • 资源1234

指定头使用-H无所谓似乎卷曲在这个特定的例子中,当不使用参数时,因为我尝试了两种方法,并且我得到了正确的XML。我不必指定-X选项,因为GET是默认的操作,这就是我所指定的操作。单引号和双引号产生相同的结果。

我试图实现GrizzlyWebContainerFactory,如24518607所述,但我无法让ant找到那个jar,尽管Eclipse很满意。我不想跟踪侧面,所以我一直专注。

想法?

+0

的信息狂野的猜测,但我猜想它与'@ GET'有关。一般来说,GET不应该有一个主体。我知道在Grizzly不支持允许DELETE的主体(不确定GET的情况)之前,但它可以被配置为允许它(可能是GET的相同配置)。但无论如何,要保持对语义的真实性,请使用POST。改变'@ POST'方法并将'-X POST'加入'curl'命令。走着瞧吧。 –

+0

@peeskillet同样的问题。我忘记补充说我尝试在testGet3调用中添加system.out.println()消息,但那些消息从未显示。灰熊从来没有调用过该方法,所以灰熊不喜欢这个方法的curl语句或格式。 –

+1

啊,我明白了。您正尝试在查询字符串中传递数据。我没有注意到这一点。在服务器上你期待它作为身体。我想你会得到一个NPE(因为没有身体),这个NPE正在被泽西吞噬,并以500的形式发出,灰熊以你所看到的形式返回。所以要修复,使用'@ POST','-X POST'而不是将数据放入URL中,请使用'-d title = smurf -d author = Gargamel' –

回答

1

首先你的资源的方法是在请求预期数据,对于它应该在curl命令使用

curl -X POST "http://localhost:8081/base/TestServer/testGet3" 
    -d "title=smurfs" -d "author=Gargamel" 

改为@POST,那么如果你想保持数据的URL与GET,那么你应该改变你的资源方法如下

@GET 
@Path("testGet3") 
@Produces(MediaType.APPLICATION_XML) 
public String testGet3(@Context UriInfo uriInfo) 
{ 
    MultivaluedMap<String, String> params 
      = uriInfo.getQueryParameters(); 
} 

或者你也可以单独列出每个查询参数与@QueryParam

public String testGet3(@QueryParam("title") String title, 
         @QueryParam("author") String author) 

顺便说一句,对于像这样的问题(其中不存在有用的错误消息),所述第一步骤I一般取是创建一个ExceptionMapper<Throwable>我注册。通常情况下,泽西岛正在吞噬一些例外情况,并且它会被一些泽西岛例外包裹,从而导致无用的500,而没有真正问题的信息。通过ExceptionMapper,在大多数情况下,它应该看到真正的异常,并且至少可以打印堆栈跟踪。请参阅here以了解关于ExceptionMapper