Jersey:将所有 POST 数据消耗到一个对象中 [英] Jersey: Consume all POST data into one object

查看:26
本文介绍了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 数据消耗到一个对象中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆