【问题标题】:Parameter array type only receive the first element JSON Jersey参数数组类型只接收 JSON Jersey 的第一个元素
【发布时间】:2017-11-19 21:43:02
【问题描述】:

这是我的 REST 服务器和 POST 方法。它有一个查询参数,它是一个字符串数组

@POST
    @Consumes({MediaType.APPLICATION_JSON})
    @Produces({MediaType.APPLICATION_JSON})
  public void create(
                @HeaderParam("authorization") String token,
                @QueryParam("title") String title, 
                @QueryParam("author") String author, 
                @QueryParam("path") String path, 
                @QueryParam("review") String review,
                @QueryParam("categories") List<String> categories
        ) {

                    System.out.println("title: " + title);
                    System.out.println("author: " + author);
                    System.out.println("path: " + path);
                    System.out.println("review: " + review);

                    for(String cat: categories) {
                        System.out.println("categories: " + categories);
                    }
    }

我尝试使用 POSTMAN 将此 json 发布到服务器

{
    "title":"HP",
    "path":"D://image/hp.jpeg",
    "author":"JK",
    "review":"this book is great",
    "categories":["fiction", "horror","science"]
}

我希望我会收到一个包含 3 个元素的字符串数组:小说、恐怖、科学,但这是我的输出:

Info:   title: HP
Info:   author: JK
Info:   path: D://image/hp.jpeg
Info:   review: this book is great
Info:   categories: [fiction]

如您所见,我的数组只有一个元素,即第一个元素。 请您指出什么是错误的以及如何解决它。谢谢

【问题讨论】:

    标签: java json rest jersey


    【解决方案1】:

    用方括号设置查询参数@QueryParam("categories[]")

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-12
      • 2020-04-07
      • 2020-12-22
      • 1970-01-01
      • 1970-01-01
      • 2014-03-25
      • 2011-07-12
      • 1970-01-01
      相关资源
      最近更新 更多