我是使用 Swagger 的新手我已经实现了一个 SwaggerConfig 类和一个 RestController。在我的 RestController 中,我只实现了 GET、POST、PUT、DELETE,但是 swagger 还生成了 OPTIONS 和 HEAD 的方法?这是什么原因?它们是否总是默认从 Swagger 生成?产生这些方法的原因是什么?提前致谢。
@RestController
public class TimesheetRequestController {
@Autowired
TimesheetRepository timeRepo;
@RequestMapping("/timesheets")
public List<Timesheet> getTimesheets() {
List<Timesheet> results = new ArrayList<>();
timeRepo.findAll().forEach(results::add);
return results;
}
@PostMapping("/timesheets")
@ApiParam(type="Timesheet")
public ResponseEntity<Object> createTimesheetEntry(@RequestBody Timesheet timesheet) {
Timesheet savedTimesheet = timeRepo.save(timesheet);
URI location = ServletUriComponentsBuilder.fromCurrentRequest().path("/{id}")
.buildAndExpand(savedTimesheet.getId()).toUri();
return ResponseEntity.created(location).build();
}
@PutMapping("/timesheets")
public ResponseEntity<Object> getTimesheetEntry(@RequestBody Timesheet timesheet, @PathVariable long id) {
Optional<Timesheet> timesheetOptional = timeRepo.findById(id);
if (!timesheetOptional.isPresent())
return ResponseEntity.notFound().build();
timesheet.setId(id);
timeRepo.save(timesheet);
return ResponseEntity.noContent().build();
}
@GetMapping("/timesheets/{id}")
public Timesheet getTimesheetEntry(@PathVariable long id) {
Optional<Timesheet> timesheet = timeRepo.findById(id);
if (!timesheet.isPresent())
throw new TimesheetNotFoundException("id-" + id);
return timesheet.get();
}
@DeleteMapping("/timesheets/{id}")
public void deleteTimesheetEntry(@PathVariable long id) {
timeRepo.deleteById(id);
}
}
这是我的 Swagger 配置:
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("package_name"))
.paths(PathSelectors.any())
.build();
}
}
凤凰求蛊
浮云间
相关分类