ApiResponses混淆了Swagger-UI输出,无法找到“类型”

ntjbwcob  于 2022-11-06  发布在  其他
关注(0)|答案(2)|浏览(147)

我不是一个真正的java开发者(15年前我是一个),但我试图更新我的知识,使之在这些项目中更有价值。我试图将swagger-ui合并到一个基本的rest springboot应用程序中。我得到了以下错误:
未知类型:客户拜访
我定义了以下控制器:

package controller;

import static org.springframework.web.bind.annotation.RequestMethod.GET;

@RequestMapping("/api/v1")
@RestController
public class ScenarioController {

    @Autowired
    ScenarioService scenarioService;

    @RequestMapping(value = "scenario/{scenarioIdStr}", method = GET)
    @ApiResponses(value={
        @ApiResponse(responseCode="200", description="request accepted, scenario returned", content=@Content(schema=@Schema(type="CustomerVisit"))),
        @ApiResponse(responseCode="202", description="request accepted, no scenario available to return"),
        @ApiResponse(responseCode="400", description="request not accepted, invalid input")
    })
    public ResponseEntity<?> getCustomerVisitByScenarioId(@PathVariable String scenarioIdStr) {

        try {
            int scenarioId = Integer.valueOf(scenarioIdStr);
            List<CustomerVisit> customerList = scenarioService.getScenarioById(scenarioId);

            if (customerList.size() > 0) {
                return new ResponseEntity<>(customerList, HttpStatus.OK);
            }
            return new ResponseEntity<>(customerList, HttpStatus.ACCEPTED);
        } catch (NumberFormatException e) {
            return new ResponseEntity<>( "Scenario ID must be integer.", HttpStatus.BAD_REQUEST);
        }
    }
}

和CustomerVisit Bean的名称为

package bean;

@Schema(name="CustomerVisit", description="A model of a customer")
public class CustomerVisit {

  public CustomerVisit(/* cut */) {
     /* cut - constructing class */      
  }

  /* cut getters */
}

swagger-ui出现了,对于其他方法,我没有试图在文档中提供ApiResponses,一切都正常。一旦我添加了ApiResponses,事情就开始崩溃了。如果我没有添加@Content注解,ui会将响应类型描述为{}。如果我在bean中添加了标记的类型名称-它会说该类型是未知的。我还没有'我没能找到文档来指导我这方面的工作,我希望能在正确的方向上得到推动。
对于旧版本,看起来有答案...

h7appiyu

h7appiyu1#

更新有效但丑陋的方法
1.添加了扩展ArrayList的静态类
1.揭露
使得

public static class CustomerVisitList extends ArrayList<CustomerVisit> {}

@RequestMapping(value = "scenario/{scenarioIdStr}", method = GET)
@ApiResponses(value={
        @ApiResponse(responseCode="200", description="request accepted, scenario returned", content=@Content(schema=@Schema(implementation = CustomerVisitList.class))),
        @ApiResponse(responseCode="202", description="request accepted, no scenario available to return"),
        @ApiResponse(responseCode="400", description="request not accepted, invalid input")
 })

有没有更好的办法?
这似乎是朝着正确方向迈出的一步(它不正确)-但它实际上应该返回一个列表:

@RequestMapping(value = "scenario/{scenarioIdStr}", method = GET)
@ApiResponses(value={
    @ApiResponse(responseCode="200", description="request accepted, scenario returned", content=@Content(schema=@Schema(implementation = CustomerVisit.class))),
    @ApiResponse(responseCode="202", description="request accepted, no scenario available to return"),
    @ApiResponse(responseCode="400", description="request not accepted, invalid input")
})
7gcisfzg

7gcisfzg2#

@ApiResponses(
value = {
    @ApiResponse(
      content = {
        @Content(
          mediaType = "application/json",
          array = @ArraySchema(schema = @Schema(implementation = Product.class)))
      })
  })
@GetMapping("/products")
public List<Product> getProductsList() {
    return productService.getProductsList();
}

相关问题