发送一个json数组,并将其作为一个jaxb jersey参数的类型列表接收。

时间:2022-05-01 19:33:08

Hello I am sorry if already asked but couldnt find.

对不起,我已经问过了,但是找不到。

Here is my problem, I do not know how many fields i will send to my webservice as they will be dynamic. As such i wanted to send a json array to my jersey jaxb ressource. as the objects in my json array will be a single dimensional array of strings i should be able to do the below:

这是我的问题,我不知道我将向我的webservice发送多少字段,因为它们是动态的。因此,我想向我的jersey jaxb ressource发送一个json数组。由于json数组中的对象将是一个单维字符串数组,我应该能够做到以下几点:

  @POST
    @Path("/test")
    @Produces(MediaType.APPLICATION_JSON)
    public Response InputList(@QueryParam("list") final List<String> inputList)

Here is my json array { "list": [ "hello", "world" ] }

这是我的json数组{"list": ["hello", "world"]}

This does not seems to work....

似乎这并不工作....

1 个解决方案

#1


4  

What you have now doesn't work because your JSON doesn't represent a list of strings. It represents an object that has a single property which is a list of strings. To wit:

现在的JSON不能工作,因为它不表示字符串列表。它表示具有单个属性的对象,该属性是字符串列表。即:

["hello", "world"]

Is a simple JSON data stream that can be deserialized directly into a List<String> in Java. Whereas:

是一个简单的JSON数据流,它可以直接被反序列化到Java中的List 。而:

{"list" : ["hello", "world"]}

Is a more complex data stream that needs to be deserialized into an object, for example one that looks like this:

是一个更复杂的数据流,需要反序列化为一个对象,例如如下所示:

public class ListHolder {
    private List<String> list;

    // constructors, getters/setters
}

You can then use this in your Jersey resource:

你可以在你的泽西资源中使用这个:

@POST
@Path("/test")
@Produces(MediaType.APPLICATION_JSON)
public Response InputList(@QueryParam("list") final ListHolder listHolder) {
    final List<String> list = listHolder.getList();
    // rest of code
}

#1


4  

What you have now doesn't work because your JSON doesn't represent a list of strings. It represents an object that has a single property which is a list of strings. To wit:

现在的JSON不能工作,因为它不表示字符串列表。它表示具有单个属性的对象,该属性是字符串列表。即:

["hello", "world"]

Is a simple JSON data stream that can be deserialized directly into a List<String> in Java. Whereas:

是一个简单的JSON数据流,它可以直接被反序列化到Java中的List 。而:

{"list" : ["hello", "world"]}

Is a more complex data stream that needs to be deserialized into an object, for example one that looks like this:

是一个更复杂的数据流,需要反序列化为一个对象,例如如下所示:

public class ListHolder {
    private List<String> list;

    // constructors, getters/setters
}

You can then use this in your Jersey resource:

你可以在你的泽西资源中使用这个:

@POST
@Path("/test")
@Produces(MediaType.APPLICATION_JSON)
public Response InputList(@QueryParam("list") final ListHolder listHolder) {
    final List<String> list = listHolder.getList();
    // rest of code
}