java 如何在球衣 Rest Webservice 中接受 json 数组输入

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

how to accept json array input in jersey Rest Webservice

javajsonweb-servicesrestjersey

提问by RK3

Am developing a rest webservice using Jersey.Am slightly new to webservices. I need to pass List of customer as input to rest webservice. having issue in achieving it.

我正在使用 Jersey 开发一个休息网络服务。我对网络服务有点陌生。我需要将客户列表作为输入传递给休息网络服务。在实现它时遇到问题。

Below is my customer object class

下面是我的客户对象类

@Component
public class customer {
private String customerId;
private String customerName;

And my endpoint is as below. addCust is the method that will be called on calling the webservice

我的端点如下。addCust 是调用 webservice 时将调用的方法

    @Path("/add")
    @Produces({MediaType.APPLICATION_JSON})
    @Consumes({MediaType.APPLICATION_JSON})
    public String addCust(@Valid customer[] customers){

    //And json input is as below
    {customers:{"customerId":"1","customerName":"a"},
    {"customerId":"2","customerName":"b"}}

But jersey is not able to convert json array to Array of Customers. It is returning 400. And the logs shows "no viable alternative at c". How to pass Json array as input to webservice and convert into Array or ArrayList. Any help appreciated.

但是球衣无法将 json 数组转换为客户数组。它返回 400。并且日志显示“在 c 处没有可行的替代方案”。如何将 Json 数组作为输入传递给 webservice 并转换为 Array 或 ArrayList。任何帮助表示赞赏。

回答by Maciej St?pyra

Your json is invalid, field names should be always double quotted, and arrays are placed inside [] for example:

您的 json 无效,字段名称应始终用双引号引起来,并将数组放在 [] 内,例如:

{"customers":[{"customerId":"1","customerName":"a"},
{"customerId":"2","customerName":"b"}]}

thats why Hymanson cannot unmarshall it. But this json will never fit your api. Here is an example of what You should send:

这就是Hyman逊无法解组它的原因。但是这个 json 永远不会适合你的 api。以下是您应该发送的示例:

[{"customerId":"1","customerName":"a"},{"customerId":"2","customerName":"b"}]

Another thing is that You can use collections instead of arrays:

另一件事是您可以使用集合而不是数组:

@Path("/add")
@Produces({MediaType.APPLICATION_JSON})
@Consumes({MediaType.APPLICATION_JSON})
public String addCust(@Valid List<Customer> customers){

If you want to send a json like this:

如果你想发送这样的json:

{"customers":[{"customerId":"1","customerName":"a"},
{"customerId":"2","customerName":"b"}]}

then you have to wrap everything into class with "customers" property:

然后你必须用“customers”属性把所有东西都包装到类中:

class AddCustomersRequest {
  private List<Customer> customers;

  public void setCustomers(List<Customer> customers) {
      this.customers = customers;
  }

  public void getCustomers() {
     return this.customers;
  }
}

And use it in Your API:

并在您的 API 中使用它:

@Path("/add")
@Produces({MediaType.APPLICATION_JSON})
@Consumes({MediaType.APPLICATION_JSON})
public String addCust(@Valid AddCustomersRequest customersReq){