gpt4 book ai didi

java - JAX-RS @POST 不返回字符串

转载 作者:行者123 更新时间:2023-12-01 09:31:37 24 4
gpt4 key购买 nike

我正在尝试编写 @POST 同步方法,它将接受 2 个字符串参数。当我从浏览器访问这两个链接时,我没有得到返回字符串“SUCCESS!”。似乎这些方法根本没有被调用?

http://localhost:9080/SampleWeb/resources/helloworld/sync?param1=string1&param2=string2

http://localhost:9080/SampleWeb/resources/helloworld/sync/string1/string2

@Path("/helloworld")
public class HelloWorldResource {

@POST
@Produces("text/plain")
@Path("/sync")
public String sync(
@QueryParam("param1") String param1,
@QueryParam("param2") String param2) {

//do something

return "SUCCESS!";
}

@POST
@Produces("text/plain")
@Path("/sync/{param1}/{param2}")
public String sync(
@PathParam("param1") String param1,
@PathParam("param2") String param2) {

//do something

return "SUCCESS!";
}
}

最佳答案

您说您正在从浏览器访问链接 - 您是“浏览”到 URL,还是从表单提交(或使用 javascript)显式 POST 到 URL?如果您只是“浏览”链接(即单击链接或将其放在地址栏中),您的浏览器将执行 GET,而不是 POST。

如果您使用的是 Unix 操作系统或在 Windows 上安装了curl,则可以使用curl 执行POST,并且您应该收到“成功!”回复:

 curl -X POST http://localhost:9080/SampleWeb/resources/helloworld/sync?param1=string1

关于java - JAX-RS @POST 不返回字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39359965/

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