Jax-rs(Jersey)消耗POST请求中的Json对象数组 [英] Jax-rs(Jersey) to Consumes Array of Json object in POST request

查看:524
本文介绍了Jax-rs(Jersey)消耗POST请求中的Json对象数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用jax-rs(Jersey)我尝试实现一个带有JSON对象列表的POST请求

Using the jax-rs(Jersey) I am try to implement a POST request that take a list of JSON object

//The resource look like this
@Path("/path")
@POST
@Consumes(MediaType.APPLICATION_JSON)
public void setJsonl(List<SomeObj> test) {
  //do work
  System.out.println(test);
}


//The class to define the json structure
@XmlRootElement
public class SomeObj{

private String tag;
private String value;

public String getTag() {
 return tag;
}

public void setTag(String tag) {
  this.tag = tag;
}

public String getValue() {
  return value;
}

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

当我尝试使用REST api进行测试时curl我总是收到错误的请求错误,我在这里错过了什么?

how ever when I try to test the REST api using curl I always get a "bad request" error, am I missing something here?

curl -X POST -H "Content-Type: application/json" -d '{"SomeObj":[{"tag":"abc", "value":"ghi"},{"tag":"123", "value":"456"}]}' http://{host_name}:8080/path_to_resource


推荐答案

如果您不介意更改方法的签名:

If you don't mind changing the signature of your method:

import org.json.JSONArray;

    //The resource look like this
    @Path("/path")
    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    public void setJsonl(String array){
        JSONArray o = new JSONArray(last_data);
        System.out.println(o.toString());

这篇关于Jax-rs(Jersey)消耗POST请求中的Json对象数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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