java - 有没有办法通过 GET 请求获取请求正文?

标签 java jersey

我有这个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/

相关文章:

Java Jersey ClientResponse 从 getClientResponseStatus 返回错误状态

java - JVM 是否交换堆?

java - 当我尝试使用 facebook sdk 登录我的 Android 应用程序时,我不断收到空用户和空错误。我不知道为什么

java - Spring MultipartFile 参数不尊重配置的 maxFileSize

java - 找不到媒体类型 MessageBodyReader=application/octet-stream

java - URL 绑定(bind)到 JSP 的 Jersey 问题

html - HTML 表单的模式不能为 REST 生成 DELETE

如果没有路径匹配,Java REST 有没有办法默认使用特定方法? (而不是得到 405)

java - 在eclipse中运行时找不到类异常

java - 如果我通过依赖于输入的谓词限制它,我可以计算我的流大小吗?