我有这个API:
@Path("test")
@GET
@Consumes({MediaType.APPLICATION_JSON})
@Produces({MediaType.APPLICATION_JSON})
public Parameter performTest(Parameter in) {
System.out.println(in);
}
但是 in 总是返回 null。我可以将 @GET 更改为 @POST 并且它可以工作,但我并没有真正执行创建或更新,因此使用 post 似乎很奇怪。
有没有办法通过带有 Jersey 的 GET 请求获取正文?
最佳答案
TL;DR正确的解决方案是使用 POST。
<小时/>"I can change @GET to @POST and it works but I'm not really performing an create or update so using post seems odd"
为什么这很奇怪? POST 不限于创建/更新操作。
规范 ( RFC 7231, section 4.3.3. POST ) 说:
The POST method requests that the target resource process the representation enclosed in the request according to the resource's own specific semantics. For example, POST is used for the following functions (among others):
Providing a block of data, such as the fields entered into an HTML form, to a data-handling process;
Posting a message to a bulletin board, newsgroup, mailing list, blog, or similar group of articles;
Creating a new resource that has yet to be identified by the origin server; and
Appending data to a resource's existing representation(s).
换句话来说,POST 的意思是“这里有一些数据,请帮我处理一下”。
当然,“处理”通常意味着“存储”,如创建/更新,但这并不是处理数据的唯一方法。
在您的情况下,“过程”意味着“使用这些参数运行测试”。
关于java - 有没有办法通过 GET 请求获取请求正文?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53598637/