swagger-ui中的OPTIONS,HEAD,PATCH方法,但在RestController中不存在

时间:2018-10-14 11:47:01

标签: java spring swagger swagger-ui

我是使用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 UI

这是我的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();
    }
}

2 个答案:

答案 0 :(得分:1)

像这样更改您的功能。

file = 'File.xlsx'
username=''
password=''
send_from = ''
send_to = 'recipient1 , recipient2'
Cc = 'recipient'
msg = MIMEMultipart()
msg['From'] = send_from
msg['To'] = send_to
msg['Cc'] = Cc
msg['Date'] = formatdate(localtime = True)
msg['Subject'] = ''
server = smtplib.SMTP('smtp.gmail.com')
port = '587'
fp = open(file, 'rb')
part = MIMEBase('application','vnd.ms-excel')
part.set_payload(fp.read())
fp.close()
encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment', filename='Name File Here')
msg.attach(part)
smtp = smtplib.SMTP('smtp.gmail.com')
smtp.ehlo()
smtp.starttls()
smtp.login(username,password)
smtp.sendmail(send_from, send_to.split(',') + msg['Cc'].split(','), msg.as_string())
smtp.quit()

甚至将其更改为@RequestMapping(path="/timesheets",method=RequestMethod.GET) public List<Timesheet> getTimesheets() { List<Timesheet> results = new ArrayList<>(); timeRepo.findAll().forEach(results::add); return results; }

这个与此issue类似。

答案 1 :(得分:0)

尽管它称为REST,它代表4种基本操作,但是REST客户端也通常使用HEAD和OPTIONS方法。因此Swagger也会自动列出处理这些http方法的控制器方法。

并且由于您尚未为此控制器方法定义任何方法参数:

@RequestMapping("/timesheets")
public List<Timesheet> getTimesheets() {

    List<Timesheet> results = new ArrayList<>();
    timeRepo.findAll().forEach(results::add);

    return results;
}

...所有未被其他控制器方法接受的请求(由于使用更严格的匹配参数)将在此处重定向。 Swagger会列出它们。

如果您不想允许HEAD和OPTIONS,则将@RequestMapping("/timesheets")更改为@GetMapping("/timesheets")