简体   繁体   English

Java Spring Boot: Swagger GET请求中的列表

[英]Java Spring Boot: Swagger GET List in the Request

I am trying to setup a GET request in Java Spring Boot Swagger, with a list of ProductIds in the request.我正在尝试在 Java Spring Boot Swagger 中设置 GET 请求,请求中包含 ProductIds 列表。 How can I edit the code below for this?我如何为此编辑下面的代码?

@GET
@Path("/product/{customerId}/{productIds}")
@ApiOperation(
        value = "Get Products",
        response = ProductResponse.class,
        responseContainer = "List"
)
List<ProductResponse> getProductData(
        @ApiParam(value = "customerId", required = true) @PathParam("customerId") long customerId,
        @ApiParam(value = "productIds", required = true) @PathParam("productIds") List<Long> productIds
);

Result: with CustomerId 7 and ProductIds (2,3)结果:CustomerId 7 和 ProductIds (2,3)

404 Not Found

http://localhost:50111/product-domain/api/product/7/2%2C3

Update: if I use RequestParam for ProductIds, how would I input this in swagger body?更新:如果我对 ProductIds 使用 RequestParam,我将如何在 swagger 正文中输入它? Note: any solution will work, (don't necessarily need to use RequestParam)注意:任何解决方案都可以,(不一定需要使用 RequestParam)

   @RequestParam(value="productIds", required=true) List<Long> productIds

在此处输入图像描述

In my opinion you should not use PathVariable but RequestParam (preferably RequestBody) in this case.在我看来,在这种情况下,您不应使用 PathVariable,而应使用 RequestParam(最好是 RequestBody)。

In case of using RequestParam it is should looks like:在使用 RequestParam 的情况下,它应该是这样的:

@Path("/product/{customerId}")
List<ProductResponse> getProductData(
        @ApiParam(value = "customerId", required = true) @PathParam("customerId") long customerId,
        @ApiParam(value = "productIds", required = true) @RequestParam("productIds") List<Long> productIds
);

than your url will look like: http://localhost:50111/product-domain/api/product/7?productIds=2,3比你的 url 看起来像:http://localhost:50111/product-domain/api/product/7?productIds=2,3

In case of using RequestBody it is should looks like:在使用 RequestBody 的情况下,它应该是这样的:

@Path("/product/{customerId}")
List<ProductResponse> getProductData(
        @ApiParam(value = "customerId", required = true) @PathParam("customerId") long customerId,
        @ApiParam(value = "productIds", required = true) @RequestBody("productIds") List<Long> productIds
);

than your url will look like: http://localhost:50111/product-domain/api/product/7 and your http request body should contains: [2, 3]比你的 url 看起来像:http://localhost:50111/product-domain/api/product/7 你的 http 请求正文应该包含:[2, 3]

Why I advise against using @PathParam in this case?为什么我建议不要在这种情况下使用@PathParam?

  • Url length has length limit (around 2048 characters) - so if You try pass long list in future it is can be a problem Url 长度有长度限制(大约 2048 个字符) - 所以如果您以后尝试传递长列表,这可能是个问题
  • Url needs to "normalize"/"escape" special characters, what makes url less readable for human, which is the essence of using PathParam Url 需要“规范化”/“转义”特殊字符,这使得 url 对人类来说可读性较差,这是使用 PathParam 的本质

BTW: Consider using PathVariable instead of PathParam - because PathVariable is from Spring, but PathParam is from JAX-RS and I assume you want to use Spring Boot顺便说一句:考虑使用 PathVariable 而不是 PathParam - 因为 PathVariable 来自 Spring,但 PathParam 来自 JAX-RS,我假设你想使用 Spring Boot

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM