代码之家  ›  专栏  ›  技术社区  ›  user3690467

使用Springfox swagger和弹簧靴记录Interable<T>

  •  0
  • user3690467  · 技术社区  · 5 年前

    public interface TodoRepository extends CrudRepository<Todo, Integer> {}
    

    这是我的控制器:

    @RestController
    @RequestMapping(path = "/todos")
    public class TodosController {
        @Autowired
        private TodoRepository todoRepository;
    
        @PostMapping(path = "")
        public Todo addTodo(@RequestParam String content) {
            Todo todo = new Todo();
            todo.setContent(content);
            todo.setDone(false);
            return todoRepository.save(todo);
        }
    
        @GetMapping(path = "")
        public Iterable<Todo> findAll() {
            return todoRepository.findAll();
        }
    }
    

    当我使用springfox生成swagger文档时,除了 findAll {} 而不是 Todo .

    Incorrect model

    但是当我使用 List<T> Iterable<T> 通过将以下内容添加到我的控制器中,由存储库提供:

        ...
        @GetMapping(path = "/asList")
        public List<Todo> asList() {
            var result = new ArrayList<Todo>();
            todoRepository.findAll().forEach(result::add);
            return result;
        }
    

    Correct model

    有没有办法让我得到同样的默认行为 存储库返回,这样我就不必转换为 列表<T>

    0 回复  |  直到 5 年前
        1
  •  0
  •   Bala    5 年前

    你可以用 ApiModelProperty 重写名称、描述、示例和数据类型等(使用 例子 也可以覆盖示例数据。) 对你来说

     @ApiModelProperty(dataType = "Todo")
     @GetMapping(path = "")
     public Iterable<Todo> findAll() {
         return todoRepository.findAll();
     }