@Consumes(MediaType.APPLICATION_FORM_URLENCODED)FormParameter为null

时间:2014-08-12 08:40:14

标签: java web-services http jersey jax-rs

我创建了一个泽西岛网络服务,如下所示:

@Path("/webhookservice")
public class Webhook {

    @POST
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    public Response readData(@FormParam("payload") Payload payload, @Context HttpServletRequest req) {

        // Gets client IP address
        String ipAddress = (req.getRemoteHost().equals("") || req.getRemoteHost() == null) ? "UNKNOWN" : req.getRemoteHost();

        // Persist data to DB
        Persist.saveToDb(payload.getId(), Integer.toString(payload.getTimestamp()),
                payload.getStatus(), payload.getAuth_code(),
                payload.getTotal_amount(), payload.getRequired_amount(),
                ipAddress);

        // Repsond with a HTTP 200 OK
        Response response = Response.status(200).build();
        return response;

    }

    @GET
    @Produces("text/plain")
    public String testService() {
        return "Service is up and working!";
    }

}

Payload类如下所示:

@XmlRootElement
public class Payload {

    private String id;
    private int timestamp;    
    private String status;    
    private String auth_code;    
    private int total_amount;    
    private int required_amount;

    // Getters
    @XmlElement(name = "id")
    public String getId() {
        return this.id;
    }

    @XmlElement(name = "timestamp")
    public int getTimestamp() {
        return this.timestamp;
    }

    @XmlElement(name = "status")
    public String getStatus() {
        return this.status;
    }

    @XmlElement(name = "auth_code")
    public String getAuth_code() {
        return this.auth_code;
    }

    @XmlElement(name = "total_amount")
    public int getTotal_amount() {
        return this.total_amount;
    }

    @XmlElement(name = "required_amount")
    public int getRequired_amount() {
        return this.required_amount;
    }

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

    public void setTimestamp(int timestamp) {
        this.timestamp = timestamp;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public void setAuth_code(String auth_code) {
        this.auth_code = auth_code;
    }

    public void setTotal_amount(int total_amount) {
        this.total_amount = total_amount;
    }

    public void setRequired_amount(int required_amount) {
        this.required_amount = required_amount;
    }

}

我发送的请求看起来像这样:

  

内容类型:application / x-www-form-urlencoded   方法:POST

未编码正文:

payload={"id":"123123","auth_code":"191331","required_amount":101,"timestamp":1407775713,"status":"completed","total_amount":101}

已编码正文:

payload%3D%7B%22id%22%3A%22123123%22%2C%22auth_code%22%3A%22191331%22%2C%22required_amount%22%3A101%2C%22timestamp%22%3A1407775713%2C%22status%22%3A%22completed%22%2C%22total_amount%22%3A101%7D

但是当我发送请求时,'有效载荷'对象在'Webhook'类的'readData()'函数中为空...有人能指出我正确的方向吗?

2 个答案:

答案 0 :(得分:2)

我这样做了,它正在发挥作用:

    @Path("users")
    @POST
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    @Produces(MediaType.APPLICATION_JSON)
    public Login postLogin( MultivaluedMap<String, String> personParams ) {
        String user = personParams.getFirst(USER);
        String pass = personParams.getFirst(PASSWORD);

答案 1 :(得分:2)

尝试使用@Consumes(MediaType.APPLICATION_XML)(在发送请求时也设置它),然后删除@FormParam("payload")注释。在客户端中发送有效内容时,请确保它是XML编码的(没有payload=)。

否则,如果您想坚持使用模式(将MediaType.APPLICATION_FORM_URLENCODED与XML混合),则必须手动解码编码的XML字符串。 我的意思是:

@Path("/webhookservice")
public class Webhook {

    @POST
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    public Response readData(@FormParam("payload") String payload, @Context HttpServletRequest req) {
        JAXBContext jaxbContext = JAXBContext.newInstance(Payload.class);
        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();

        StringReader reader = new StringReader(payload,);
        Payload payload = (Payload) unmarshaller.unmarshal(reader);

        //...
    }
}

为什么必须使用String而不是Payload参数?因为没有人告诉Jersey / JAX-RS如何解码该字符串:它可能是JSON字符串或XML字符串。