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 数据消耗到一个对象中
基础教程推荐
- 在java中使用xpath和selenium解析HTML表格数据 2022-01-01
- 将 double 转换为 Int,向下舍入 2022-01-01
- 控制台应用程序中的 Java 键盘输入解析 2022-01-01
- 将 Windows 证书导入 Java 2022-01-01
- 在springboot中如何给mybatis加拦截器 2023-04-29
- Java ECDSAwithSHA256 签名长度不一致 2022-01-01
- 如何在相机中应用自定义滤镜 [Surfaceview 预览]. 2022-01-01
- Maven:无效的目标版本:10 2022-01-01
- JPA惰性列表上的流 2022-01-01
- doFilter()是在servlet的工作完成之前还是之后执行的? 2022-01-01
