问题
I'm using swagger2 in my spring boot project. It's working well, but I need to exclude the basic-error-controller
from the api. Currently I'm using the following code using regex. It's working but is there any perfect way to do this.
CODE :
@Bean
public Docket demoApi() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.regex('(?!/error.*).*'))
.build()
}
回答1:
After searching in google I got the solution from one issue in GitHub, [question] How to exclude the basic-error-controller from being added to the swagger description?. It can be done using Predicates.not().
Code looks like as follows after using Predicates.not().
@Bean
public Docket demoApi() {
return new Docket(DocumentationType.SWAGGER_2)//<3>
.select()//<4>
.apis(RequestHandlerSelectors.any())//<5>
.paths(Predicates.not(PathSelectors.regex("/error.*")))//<6>, regex must be in double quotes.
.build()
}
回答2:
Lot's of time gone by, but if someone has same problem you could do it by providing selector for RestController:
new Docket(SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.withClassAnnotation(RestController.class))
.paths(PathSelectors.any())
.build();
Keeping in mind that your controllers are annotated with @RestController
回答3:
If you are using a custom ErrorController
just annotate it with
@ApiIgnore
or
@Api(hidden = true)
for example:
@Controller
@ApiIgnore
class MyErrorController : ErrorController {
@RequestMapping("/error")
fun handleError(request: HttpServletRequest): String {
val status: String? = request.getAttribute(RequestDispatcher.ERROR_STATUS_CODE)?.toString()
val statusCode: Int? = status?.toInt()
return when (statusCode) {
HttpStatus.NOT_FOUND.value() -> return "error-404"
HttpStatus.INTERNAL_SERVER_ERROR.value() -> return "error-500"
else -> "error"
}
}
override fun getErrorPath(): String {
return "/error"
}
}
回答4:
The best way I found of limiting the endpoints that are displayed by the swagger documentation is doing this:
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(paths())
.build().apiInfo(metadata());
}
private Predicate<String> paths() {
return or(
regex("/firstContext.*"),
regex("/secondContext.*"));
}
private ApiInfo metadata() {
return new ApiInfoBuilder()
.title("SomeTitle")
.description("SomeDescription")
.build();
}
So each endpoint that does not start with the paths() method contexts will not be rendered by swagger
回答5:
I encountered the same problem. I did this.
java
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("com.xxx.xxx"))
.paths(PathSelectors.any())
.build();
}
回答6:
What I think you should do is write some regex that matches all your API endpoints, if you are running microservices then that will probably be just one-word match if you don't then perhaps something that you put in the question makes more sense to me.
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.regex("/accounts.*"))
.build();
}
回答7:
In my case when I make a method as @Bean than it will not show basic-error-controller.
If I remove @Bean it will show basic-error-controller in swagger-ui.
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2).select()
.apis(RequestHandlerSelectors.basePackage(CONTROLLER_PATH))
.paths(regex("/.*")).build();}
来源:https://stackoverflow.com/questions/33431343/avoiding-default-basic-error-controller-from-swagger-api