java Jax-rs(Jersey) 在 POST 请求中使用 Json 对象数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11237102/
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-10-31 04:20:36  来源:igfitidea点击:

Jax-rs(Jersey) to Consumes Array of Json object in POST request

javarestjerseyjax-rs

提问by LOK

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

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

//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;
}
}

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 测试 REST api 时,我总是收到“错误请求”错误,我是否在这里遗漏了什么?

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

回答by user311174

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

回答by Harun Dalo?lu

a late answer but may be helpful for others Post this:

一个迟到的答案,但可能对其他人有帮助 发布这个:

[{"tag":"abc", "value":"ghi"},{"tag":"123", "value":"456"}]

[{"tag":"abc", "value":"ghi"},{"tag":"123", "value":"456"}]

Because by sending this:

因为通过发送这个:

{"SomeObj":[{"tag":"abc", "value":"ghi"},{"tag":"123", "value":"456"}]}

{"SomeObj":[{"tag":"abc", "value":"ghi"},{"tag":"123", "value":"456"}]}

you are posting an object with a single 'SomeObj' named property. you are not posting an array

您正在发布具有单个“SomeObj”命名属性的对象。你没有发布一个数组

回答by guilhebl

Try wrapping your JSON array inside an object like:

尝试将您的 JSON 数组包装在一个对象中,例如:

@XmlRootElement 
public class SomeObjListWrapper {
private List<SomeObj> list;
// getters and setters
}

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

回答by F. P. Freely

On the server side:

在服务器端:

import _root_.org.codehaus.jettison.json.{JSONArray, JSONObject}
@POST
@Path("/wants-json-array")
@Consumes(Array(MediaType.APPLICATION_JSON))
def wantsJSONArray(array: JSONArray): Response =
{
    // here's your array
}

And on the client side:

而在客户端:

$.ajax(
{
    type: "GET",
    url: '/your-web-service/wants-json-array',
    data: JSON.stringify(THEARRAYYOUARESENDINGTOTHESERVER),
    contentType: "application/json",
    dataType: "json",
    processData: false
});