java Jersey:将所有 POST 数据消耗到一个对象中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17630788/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 02:31:52  来源:igfitidea点击:

Jersey: Consume all POST data into one object

javarestjerseyunmarshalling

提问by Mono Jamoon

I am using Jersey 1.8 in my application. I am trying to consume POSTdata 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 1.8。我正在尝试POST在服务器上使用数据。数据类型为application/x-www-form-urlencoded有没有一种方法可以获取一个对象中的所有数据,也许是一个Map<String, Object>.

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:

我遇到了泽西岛的@Consumes(MediaType.APPLICATION_FORM_URLENCODED). 但是使用它需要我使用@FormParam,如果参数数量很大,这可能会很乏味。或者也许一种方法是这样的:

    @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();
    }

The above code consumes and presents the form data in a Stringobject.

上面的代码在一个String对象中消费和呈现表单数据。

_search=false&nd=1373722302667&rows=10&page=1&sidx=email&sord=desc

Processing this can be error prone as any misplaced &and split() will return corrupt data.

处理这个可能容易出错,因为任何错位&和 split() 将返回损坏的数据。

I used UriInfo for most of my work which would give me the query parameters in a MultiValuedMapor for other POST requests, sent the payload in jsonformat 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.

我在大部分工作中都使用了 UriInfo,它会为我提供 aMultiValuedMap或其他 POST 请求中的查询参数,以json格式发送有效负载,然后将其解组为Map<String, Object>. 如果 POST 数据的类型为application/x-www-form-urlencoded.

回答by Mono Jamoon

Got it. As per thisdocument, I can use a MultivaluedMap<K,V>or Formto get all the POST data of the type application/x-www-form-urlencodedin one object. A working exmple:

知道了。根据文档,我可以使用 aMultivaluedMap<K,V>Form来获取application/x-www-form-urlencoded一个对象中该类型的所有 POST 数据。一个工作示例:

    @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();
    }

回答by Jianxin Gao

For cases where the Content-Typeis multipart/form-data, this MultivaluedMapapproach won't work.

对于Content-Typeis 的情况multipart/form-data,这种MultivaluedMap方法不起作用。

Use FormDataMultiPart- docs here.

使用FormDataMultiPart-文档在这里