Spring请求Params与Swagger

时间:2018-06-04 19:31:23

标签: spring rest swagger swagger-ui swagger-2.0

这是我现在的代码:

    @GetMapping
public ResponseEntity<List<EmployeeDTO>> getAllEmployees() {
    List<EmployeeDTO> employees = employeeManager.getEmployees();

    if(employees.isEmpty()) {
        return new ResponseEntity(new CustomError("No employees in database. "), HttpStatus.NO_CONTENT);
    }

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

@RequestMapping(method = RequestMethod.GET, params = {"firstName", "lastName"})
public ResponseEntity<List<EmployeeDTO>> getEmployeesByName(
        @ApiParam(name = "firstName", value = "first name")
        @RequestParam(value = "firstName", required = false) String firstName,
        @ApiParam(name = "lastName", value = "last name")
        @RequestParam(value = "lastName", required = false) String lastName) {
    List<EmployeeDTO> employees = employeeManager.filterByFirstName(firstName);

    if(employees.isEmpty()) {
        return new ResponseEntity(new CustomError("No employees with given first name found. "), HttpStatus.NOT_FOUND);
    }

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

我想要实现的目标: 带有UI的swagger可选的名字和姓氏字段。 目前它们要么在UI中消失,要么都是必需的。它可行吗?

0 个答案:

没有答案