Jersey:将所有POST数据都合并到一个对象中 [英] Jersey: Consume all POST data into one object
问题描述
我在我的应用程序中使用Jersey 1.8。我试图在服务器上使用 POST
数据。数据类型为 application / x-www-form-urlencoded
。 是否有方法可以在一个对象中获取所有数据,可能是 Map< String,Object>
。
I am using Jersey 1.8 in my application. I am trying to consume POST
data at the server. The data is of the type application/x-www-form-urlencoded
. Is there a method to get all the data in one object, maybe a Map<String, Object>
.
我遇到泽西岛的 @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
。但是使用它需要我使用 @FormParam
,如果参数数量很大,这可能会很乏味。或者也许有一种方式:
I ran into Jersey's @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
. But using this would require me to use @FormParam
, which can be tedious if the number of parameters are huge. Or maybe one way is this:
@POST
@Path("/urienodedeample")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Produces(MediaType.APPLICATION_JSON)
public Response uriEncodedExample(String uriInfo){
logger.info(uriInfo);
//process data
return Response.status(200).build();
}
以上代码使用并以 String
object。
The above code consumes and presents the form data in a String
object.
_search=false&nd=1373722302667&rows=10&page=1&sidx=email&sord=desc
处理此问题可能容易出错,因为任何错位&
和split()将返回损坏的数据。
Processing this can be error prone as any misplaced &
and split() will return corrupt data.
我在大多数工作中都使用了UriInfo,它会在 MultiValuedMap
或其他POST中提供查询参数请求,以 json
格式发送有效负载,然后将其解组为 Map< String,Object>
。如果POST数据的类型是 application / x-www-form-urlencoded
,那么我对如何做同样的建议。
I used UriInfo for most of my work which would give me the query parameters in a MultiValuedMap
or for other POST requests, sent the payload in json
format which would in turn be unmarshalled into a Map<String, Object>
. Any suggestions on how I can do the same if the POST data is of the type application/x-www-form-urlencoded
.
推荐答案
知道了。根据此文档,我可以使用多值地图< K ,V>
或表格在一个对象中获取 application / x-www-form-urlencoded
类型的所有POST数据。一个工作的例子:
Got it. As per this document, I can use a MultivaluedMap<K,V>
or Form to get all the POST data of the type application/x-www-form-urlencoded
in one object. A working exmple:
@POST
@Path("/urienodedeample")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Produces(MediaType.APPLICATION_JSON)
public Response uriEncodedExample(MultivaluedMap<String,String> multivaluedMap) {
logger.info(multivaluedMap);
return Response.status(200).build();
}
这篇关于Jersey:将所有POST数据都合并到一个对象中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!