Jersey: Consume all POST data into one object(Jersey:将所有 POST 数据消耗到一个对象中)
问题描述
我在我的应用程序中使用 Jersey 1.8.我正在尝试在服务器上使用 POST 数据.数据的类型为 application/x-www-form-urlencoded.有没有一种方法可以获取一个对象中的所有数据,可能是 Map.
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>.
我遇到了 Jersey 的 @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 对象中消费和呈现表单数据.
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.如果 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.
推荐答案
知道了.根据 this 文档,我可以使用 MultivaluedMap<K,V> 或 Form 来获取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 数据消耗到一个对象中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Jersey:将所有 POST 数据消耗到一个对象中
基础教程推荐
- Struts2 URL 无法访问 2022-01-01
- 修改 void 函数的输入参数,然后读取 2022-01-01
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13
- RabbitMQ:消息保持“未确认"; 2022-01-01
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01
- 使用堆栈算法进行括号/括号匹配 2022-01-01
- 如何对 Java Hashmap 中的值求和 2022-01-01
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01
- 存储 20 位数字的数据类型 2022-01-01
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01
