Spring Boot Actuator / Swagger - spring-boot

I'm working on Spring Boot application and i use Swagger for the documentation.
I have adding Spring Boot Actuator on my application, but now i want to add the new services creating by actuator (/health /metrics ..) on my swagger documentation.
I don't find how configure Actuator and Swagger.

You can configure in Swagger which paths you want to be added to the documentation :
#Bean
public Docket appApi() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
...
}
will display all available endpoints.
.paths(PathSelectors.any("/mypath/**")) will limit only to endpoints exposed in mypath.

In order to display spring-boot-actuator endpoints in springdoc-openapi, simply add the following property:
springdoc.show-actuator=true

Update: 26-04-2017, updated implemention. Credits to Andy Brown for the tip.
Due to our coding convention, we don't have a specific prefix for our endpoints, so I was looking for a solution to exclude the actuator endpoints, rather than to include my own paths.
I've came up with the following config to only exclude the actuator endpoints.
This way, I don't have to update the config once I add new endpoints nor do I have to prefix my own endpoints to distinguish them from the actuator endpoints.
/**
* This enables swagger. See http://localhost:8080/v2/api-docs for the swagger.json output!
* #param actuatorEndpointHandlerMapping this endpoint handler mapping contains all the endpoints provided by the
* spring actuator. We will iterate over all the endpoints and exclude them from the swagger documentation.
* #return the docket.
*/
#Autowired
#Bean
public Docket swaggerSpringMvcPlugin(final EndpointHandlerMapping actuatorEndpointHandlerMapping) {
ApiSelectorBuilder builder = new Docket(DocumentationType.SWAGGER_2)
.useDefaultResponseMessages(false)
.apiInfo(apiInfo())
.securitySchemes(securitySchemes())
.select();
// Ignore the spring-boot-actuator endpoints:
Set<MvcEndpoint> endpoints = actuatorEndpointHandlerMapping.getEndpoints();
endpoints.forEach(endpoint -> {
String path = endpoint.getPath();
log.debug("excluded path for swagger {}", path);
builder.paths(Predicates.not(PathSelectors.regex(path + ".*")));
});
return builder.build();
}

ApiSelectorBuilder builder = new Docket(DocumentationType.SWAGGER_2)
.useDefaultResponseMessages(false)
.apiInfo(apiInfo())
.securitySchemes(securitySchemes())
.select()
.apis(RequestHandlerSelectors.any())
.paths(Predicates.not(PathSelectors.regex("/actuator.*")))
.build();
Hi, You can exclude path on regex and you can chain them.

Move the actuator endpoints into a context path through your application.properties file.
management.context-path=/manage
Then you can exclude that path from swagger
#Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(Predicates.not(PathSelectors.regex("/manage.*")))
.build();
}
You might want to exclude the error controller too
#Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(Predicates.not(PathSelectors.regex("(/manage.*|/error)")))
.build();
}

Alternatives:
a) Exclude path with a NOT in the Regex?
#Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.regex("^((?!/error).)*")) // exclude Basic Error Controller
.build();
}
b) or chain and negate a Predicate:
PathSelectors.any()
.and(PathSelectors.regex("/error").negate())
.and(PathSelectors.regex("/manage.*").negate());

Related

Change Swagger UI request URL when I tap to "Execute" button

I deployed my app on AWS. Also, I configured Nginx that /api prefix (https://mywebsitename.com/api) will be automatically redirected to https://127.0.0.1:8080/.
Every HTTP request & response works perfectly.
Also, I added the swagger library to my app, with following configuration:
#Configuration
#EnableSwagger2
class SwaggerConfig {
#Bean
fun api(): Docket {
return Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build()
}
}
I use this two libraries:
implementation("io.springfox:springfox-swagger2:2.9.2")
implementation("io.springfox:springfox-swagger-ui:2.9.2")
Everything works perfectly locally.
The problem is:
The swagger link (https://mywebsitename.com/api/swagger-ui.html) works perfectly, and everything is shown. But, when I tap to the "Try it out" button and make "Execute", it returns me TypeError: Failed to fetch. It happens because the request goes to https://127.0.0.1:8080/some_endpoint.
Question is: How I can configure my spring boot app that when I tap to "Execute" button, the request will go to https://mywebsitename.com/api/some_end_point
Have you tried adding the host call in the Swagger configuration like this:
#Configuration
#EnableSwagger2
class SwaggerConfig {
#Bean
fun api(): Docket {
return Docket(DocumentationType.SWAGGER_2)
.host("https://mywebsitename.com")
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build()
}
}
you can try adding servers in Swagger Configuration like this:
#Bean
public Docket api() {
Server serverLocal = new Server("local", "http://localhost:8080", "for local usages", Collections.emptyList(), Collections.emptyList());
Server testServer = new Server("test", "https://example.org", "for testing", Collections.emptyList(), Collections.emptyList());
return new Docket(DocumentationType.SWAGGER_2)
.servers(serverLocal, testServer)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}

Springfox: How to manually add api paths to swagger-ui?

When configuring a Docket we have to specify the base package where to look for api endpoints. Is it possible to add manually an endpoint which is not mapped to any controller?
I have a legacy application which is a simple Zuul server but it has a swagger-ui, the goal is to specifiy endpoints that are proxied by zuul?
#Bean
public Docket docket() {
return new Docket(DocumentationType.SWAGGER_2)
.globalOperationParameters(getHeaders()).select()
.apis(RequestHandlerSelectors.basePackage("package.controllers"))
.additionalApis(Method.PUT, "/my/manual/endpoint") // This does not exist
.paths(PathSelectors.any())
.build();
}
Thanks

Consider zuul route in Swagger documentation

is it possible to consider the zuul filter (manual or automatic) to show in the swagger?
The issue is the following:
we have a microservice which is the single point of entry for our application and which holds all swagger documentation. in fact it works as a fasade, so it routes the request to the corresponding microservices inside our application. Unfortunately, zuul sometimes it adds something to the route or removes something. therefore the swagger documentation is incorrect, as the base-path is slightly different.
is there any possiblity to influence this?
swagger docket is used with simple lookup:
#Bean
public Docket swaggerSpringfoxDocket() {
this.log.debug("Starting Swagger");
StopWatch watch = new StopWatch();
watch.start();
Docket docket = new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.forCodeGeneration(false)
.select()
.paths(regex("/api/.*"))
.build();
watch.stop();
this.log.debug("Started Swagger in {} ms", watch.getTotalTimeMillis());
return docket;
}

Swagger2 not dispaying the documentation is UI format

I have RESTful web service which I have developed in spring boot. I have integrated the swagger2 in my application using Gradle build tool.
testCompile('io.springfox:springfox-swagger2:2.6.1')
testCompile('io.springfox:springfox-swagger-ui:2.6.1')
I wrote the configuration file for swagger2 in following way
#Configuration
#EnableSwagger2
public class SwaggerConfig {
#Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select().apis(RequestHandlerSelectors.basePackage("com.example.restdemo.web"))
.paths(PathSelectors.any())
.build();
}
}
Now when I try to access the http://localhost:8080/v2/api-docs I am getting the JSON string. But when I am trying to access the http://localhost:8080/swagger-ui.html I am not getting Swagger UI view, I am getting the 406 error.
Did you try like this?
http://localhost:8080/swagger-ui.html#!/test-controller/
Here Controller class name is TestController
Also, replace
.select().apis(RequestHandlerSelectors.basePackage("com.example.restdemo.web"))
with
.select()
As below..
#Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}
Can you try below Swagger configurations? basePackage is nothing but the entry point of your rest API layer. You can hardcode it in your program.
#Configuration
#EnableSwagger2
public class SwaggerConfig {
#Bean
UiConfiguration uiConfig() {
return new UiConfiguration("validatorUrl", "list", "alpha", "schema",
UiConfiguration.Constants.DEFAULT_SUBMIT_METHODS, false, true, 60000L);
}
}

How do you configure the title, description and license in Springfox Swagger UI?

When you start Swagger UI with Springfox in a Spring Boot app, it looks like this:
How do you configure the title and description ("Api Documentation") and the license (Apache 2.0).
You can set these values by passing the ApiInfo object to your docket.
new Docket(DocumentationType.SWAGGER_2)
...
.apiInfo(new ApiInfo(...))
...
ApiInfo's constructor accepts several details about your API. In you case, you should look at title, description, and license parameters.
Swagger config class (Spring boot):
#Configuration
public class SpringFoxConfig {
#Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}
ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("Demo Test API")
.description("Demo test API task")
.license("© 2021 by my")
.build();
}
}

Resources