Springfox Swagger将响应状态200添加到POST和PUT

时间:2016-11-20 09:55:31

标签: java spring swagger springfox

我正在使用springfox-swagger2版本2.6.1,它会自动为PUT和POST操作插入HTTP 200响应消息,尽管我试图将其配置为不这样做(我不使用响应状态200进行POST或PUT,但分别为201和204);见下面的截图:

enter image description here

我已经看到类似问题的答案,其中作者建议在控制器中添加@Test public void testMyPrinteService() { // select default or specific one here PrintService printService = PrintServiceLookup.lookupDefaultPrintService(); PrinterJob job = PrinterJob.getPrinterJob(); PageFormat pf = job.defaultPage(); Paper p = pf.getPaper(); p.setSize(UnitConv.mm2pt(62), UnitConv.mm2pt(40)); p.setImageableArea(0, 0, UnitConv.mm2pt(62), UnitConv.mm2pt(40)); pf.setPaper(p); pf.setOrientation(PageFormat.LANDSCAPE); Printable myPrintable = null ;//some printable new MyPrintable(1); PrintRequestAttributeSet printReqAttrs = new HashPrintRequestAttributeSet(); PrintRequestAttribute jobPrinArea = new MediaPrintableArea(0, 0, 64, 40, MediaPrintableArea.MM); printReqAttrs.add(jobPrinArea); if (job.printDialog(printReqAttrs)) { try { job.setPrintable(myPrintable, pf); job.setPrintService(printService); //pass specific attributes to printer job.print(printReqAttrs); } catch (Exception PrintException) { PrintException.printStackTrace(); } } } 注释以修复"但是,这变得不灵活,违反了Spring自己关于使用@ResponseStatus vs ResponseEntity用于其他API的文档。例子:

How to change the response status code for successful operation in Swagger?

https://github.com/springfox/springfox/issues/908

有没有其他方法可以强制Springfox Swagger不添加200 OK状态代码?

我的Docket配置:

@ResponseStatus

...以及实际的API端点声明:

@Bean
public Docket api() {
    return new Docket(DocumentationType.SWAGGER_2)
            .useDefaultResponseMessages(false)
            .select().
                    apis(RequestHandlerSelectors.any()).
                    paths(paths()).
                    build()
            .pathMapping("/")
            .apiInfo(apiInfo())
            .genericModelSubstitutes(ResponseEntity.class)
            .alternateTypeRules(newRule(
                    typeResolver.resolve(DeferredResult.class, typeResolver.resolve(ResponseEntity.class, WildcardType.class)),
                    typeResolver.resolve(WildcardType.class)
            ));

1 个答案:

答案 0 :(得分:0)

尝试添加@ResponseStatus(HttpStatus.CREATED)@ResponseStatus(HttpStatus.NO_CONTENT)注释。取自here