今天,当我通过Jersey文档时,我遇到了以下声明
Unlike method parameters that are associated with the extraction of request parameters,
the method parameter associated with the representation being consumed does not require
annotating. A maximum of one such unannotated method parameter may exist since there
may only be a maximum of one such representation sent in a request.
我是JAX-RS的新手因此我们在请求中发送这样一个参数的方式不太清楚(我没有找到任何具体的例子来更好地理解)
从上面的陈述我理解的是,我们可以有一些资源方法
@Path("restful")
public class MyResource{
...
@GET
@Produces("application/text")
public String getStringResp(String param){
...
return "some value";
}
....
}
这里我们没有使用任何带注释的参数,如路径,矩阵,查询或任何其他参数。
现在我的问题是,在客户端,我们如何向方法参数“param”发送一个值?如果param被分别注释,我们可以在“webtarget”或“invocationBuilder”上使用queryparam()等api方法发送请求参数。事实并非如此。
请帮助我理解这个?
提前致谢
首先要理解的是请求主要有两个部分,主体和头部。你发布的文档是什么,是一个无注释参数,最终是请求的主体。
你通常不会发送任何有GET
请求的实体,但是对于PUT
和POST
,有put(Entity<?> entity)
和post(Entity<?> entity)
,Ivocation.Builder
继承自SyncInvoker
。
Entity
类具有静态方法,我们可以从中形成实体主体。例如
// application/json data
target.request().post(Entity.json(jsonStringDataOrPojo));
// application/xml data
target.request().post(Entity.xml(xmlStringDataOrPojo));
// text/plain data
target.request().post(Entity.entity(stringData, MediaType.TEXT_PLAIN));
// text/plain data
target.request().post(Entity.text(stringData));
在你的情况下,使用String
,你实际上可以发送其中任何一个。由于您尚未在资源方法上指定@Consumes
批注,因此它可以是纯文本xml或json。对于xml和json,您只需以原始形式获取数据。
如果你有
@POST
@Consumes(MediaType.TEXT_PLAIN)
public Response postString(String param){
然后你需要发送纯文本,即上面最后一个例子
你真的应该使用@PathParam(“param”)方法param,比如@PathParam(“param”)param,如果你想了解更多,我建议看看源代码。