Java Swagger无法生成端点响应类型为List <T>的服务

时间:2019-12-08 17:19:04

标签: java spring-boot swagger swagger-ui

我正在尝试在Java中使用swagger。 使用NSwag studio,我能够生成我的所有端点,除了一个返回对象列表的端点。 这是我在控制器中的操作:

@ApiOperation(value = "getAll", nickname = "getAll", responseContainer = "List", response = DiakEntity.class)
@GetMapping("/api/diakok")
@ResponseBody
@PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_CLIENT')")
public List<DiakEntity> GetDiakok() throws Exception
{
    ServiceObjectResponse<List<DiakEntity>> request = _diakService.getAll();

    if(!request.getIsSuccess())
    {
        throw new Exception(request.getMessage());
    }
    return request.getObject();
}

我正在使用swagger注释1.5.23,springfox-swagger-ui 2.9.2,springfox-swagger2 2.9.2。 如果我从邮递员测试,那就行了。

也尝试这样:

@ApiOperation(value = "getAll", nickname = "getAll")
    @ApiResponse(code = 200, responseContainer="List", response=DiakEntity.class, message = "Gets all diak objects")
    @GetMapping("/api/diakok")
    @ResponseBody
    @PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_CLIENT')")
    public ResponseEntity<List<DiakEntity>> GetDiakok() throws Exception
    {
        ServiceObjectResponse<List<DiakEntity>> request = _diakService.getAll();

        if(!request.getIsSuccess())
        {
            throw new Exception(request.getMessage());
        }

        return new ResponseEntity<>(request.getObject(), HttpStatus.OK);
    }

thnx

2 个答案:

答案 0 :(得分:0)

请尝试使用以下注释来招摇。

@ApiOperation(value = "getAll", nickname = "getAll")
@ApiResponse(code = 200, responseContainer="List", response=DiakEntity.class)

答案 1 :(得分:0)

最后,我按如下所示更改了操作,然后它开始起作用

@ApiOperation(value = "all", nickname = "all")
    @PostMapping("/api/diak/all")
    @ResponseBody
    @PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_CLIENT')")
    public List<DiakEntity> GetAll(@RequestBody @Valid RequestDiakByName data) throws Exception
    {
        ServiceObjectResponse<List<DiakEntity>> request = _diakService.getAll();

        if(!request.getIsSuccess())
        {
            throw new Exception(request.getMessage());
        }
        return request.getObject();
    }