gpt4 book ai didi

java - GrizzlyHttpServerFactory.createHttpServer @GET 带参数

转载 作者:行者123 更新时间:2023-11-30 07:48:38 25 4
gpt4 key购买 nike

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

enter image description here

我仔细阅读了几个例子,一切看起来都很好。只要我不尝试传递参数,一切都会正常。这是我到目前为止所拥有的代码。

在 main() 中

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 机器上运行后。这是终端命令。

工作(无参数):

[test@Turbo 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>

带参数不起作用:

[test@Turbo 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>[test@Turbo Downloads]$

在传递参数时,我确实将 url 括在引号中,所以这不是问题。想法?

以下是我仔细阅读的一些资源:

  • 资源 1 , 2 , 3 , 4

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

我尝试实现 GrizzlyWebContainerFactory,如 24518607 中所述。 ,但我无法让 ant 找到那个 jar,尽管 Eclipse 很高兴。我不想分心,所以我保持专注。

想法?

最佳答案

首先,您的资源方法需要请求正文中的数据,因此应将其更改为 @POST ,然后在 curl命令使用

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

如果您想通过 GET 保留 URL 中的数据,那么您应该将资源方法更改为以下

@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)

顺便说一句,对于像这样的问题(没有有用的错误消息),我通常采取的第一步是创建一个 ExceptionMapper<Throwable>我注册的。通常有一些异常被 Jersey 吞没,并且它被包裹在一些 Jersey 异常中,这会导致无用的 500,没有真正问题的消息。随着ExceptionMapper ,在大多数情况下它应该看到真正的异常,并且至少可以打印堆栈跟踪。请参阅here有关 ExceptionMapper 的信息

关于java - GrizzlyHttpServerFactory.createHttpServer @GET 带参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33576243/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com