首页 > 解决方案 > 当 api 定义在单独的接口上时,两次生成 Swagger

问题描述

我通过codegen生成了以下界面:

/**
 * POST /pet : Add a new pet to the store
 *
 * @param petModel Pet object that needs to be added to the store (required)
 * @return Invalid input (status code 405)
 */
@ApiOperation(value = "Add a new pet to the store", nickname = "addPet", notes = "", tags={ "private", })
@ApiResponses(value = { 
    @ApiResponse(code = 405, message = "Invalid input") })
@RequestMapping(value = "/private/pet",
    consumes = { "application/json" },
    method = RequestMethod.POST)
default ResponseEntity<Void> addPet(@ApiParam(value = "Pet object that needs to be added to the store" ,required=true )  @Valid @RequestBody PetModel petModel) throws Exception {
    return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);

}

然后我让我的控制器实现它:

public class PetController implements Api{

    public ResponseEntity<Void> addPet(...
}

我还定义了这个案卷:

@Bean
public Docket myApi(ServletContext servletContext) {
    return new Docket(DocumentationType.SWAGGER_2).pathProvider(new DefaultPathProvider() {
        @Override
        public String getOperationPath(String operationPath) {
            return StringUtils.substringAfter(super.getOperationPath(operationPath),contextPath) ;
        }

    })
            .select()
            .apis(RequestHandlerSelectors.withClassAnnotation(RestController.class))
            .paths(PathSelectors.any())
            .build();
}

@Bean
public WebMvcConfigurer webMvcConfigurer()
{
    return new WebMvcConfigurer()
    {
        @Override
        public void addResourceHandlers( ResourceHandlerRegistry registry )
        {
            registry.addResourceHandler( "swagger-ui.html" ).addResourceLocations( "classpath:/META-INF/resources/" );
            registry.addResourceHandler( "/webjars/**" ).addResourceLocations( "classpath:/META-INF/resources/webjars/" );
        }
    };
}

它可以工作,但是在产生的招摇中,我得到了一个与我的控制器相关的空行。我可以摆脱它吗?

在此处输入图像描述

标签: spring-bootswaggerswagger-ui

解决方案


推荐阅读