如何在cxf java中将查询字符串反序列化为pojo

时间:2014-11-16 17:29:58

标签: java cxf deserialization query-string

我正在尝试将查询字符串转换为Pojo。有没有办法将查询字符串转换为pojo。以下是我到目前为止所尝试的内容:

请求:

POST /Service/services/handler/qpStringtoPOJO HTTP/1.1
Host: localhost
Cache-Control: no-cache

id=123434&value=BB$@##@FDBB$#Gw3r3232rvd

POJO的:

    package com.nexsols.license.model;

    import javax.xml.bind.annotation.XmlElement;
    import javax.xml.bind.annotation.XmlRootElement;

    @XmlRootElement
    public class Pojo {
        String id;
        String value;
        @XmlElement(name="id")
        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        @XmlElement(name="value")
        public String getValue() {
            return value;
        }

        public void setValue(String value) {
            this.value = value;
        }

        @Override
        public String toString() {
            return "Pojo [id=" + id + ", value=" + value + "]";
        }
    }

服务方式:

@POST
@Path("/qpStringtoPOJO")
public String qpStringtoPOJO(@QueryParam("") Pojo pojo){

    System.out.println(pojo);
    return "SUCCESS";
}

目前的输出是 Pojo [id = null,value = null]

我正在使用cxf服务。我做错了什么? 在此先感谢。

2 个答案:

答案 0 :(得分:0)

鉴于您在下面的评论中所述的限制,以下是我将如何解决这个问题:

  1. 使用@Context批注获取HTTP请求对象
  2. 从请求对象中提取感兴趣的参数
  3. 自己构建POJO
  4. 试试这个......

    @POST
    @Path("/qpStringtoPOJO")
    public String qpStringtoPOJO(@Context HttpServletRequest httpRequest){      
       String id = request.getParameter("id");
       String value = request.getParameter("value");
       Pojo pojo = new Pojo();
       pojo.setId(id):
       pojo.setValue(value);
    }
    

    或者这(虽然我没有测试过这个):

    @POST
    @Path("/qpStringtoPOJO")
    public String qpStringtoPOJO(@FormParam("id") String id, @FormParam("value") String value){     
       Pojo pojo = new Pojo();
       pojo.setId(id):
       pojo.setValue(value);
    }
    

答案 1 :(得分:0)

您也可以使用map并从request.getParameterMap()获取它。 然后,您可以将所需的密钥传递给此映射以获取其值。