Spring Boot + Swagger-Ui yml generate - spring-boot

Create a swagger-ui with the yaml file generated by swagger-editor
Creating an auto swagger-ui using Annotation was successful.
But I do not know how to create Swagger-ui with yaml created with swagger-editor. Is there anyone who can explain in detail? The development environment uses SpringBoot 2.0.4.
#Configuration
#EnableSwagger2
public class SwaggerConfig {
#Bean
public Docket getApi() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(paths())
.build();
}
private Predicate<String> paths() {
return Predicates.and(
PathSelectors.regex("/shop.*"),
Predicates.not(PathSelectors.regex("/error.*")));
}
}
The above code can make all the APIs inside the server Swagger-ui.
However, I would like to configure swagger-ui with the yaml file that I wrote myself through the swagger-editor.

Related

Adding base path to swagger documentaion

I am trying to change the base path of swagger codumentation. Currently I have
#RequestMapping(path = "/api/resourceName/v1")
and swagger config
return new Docket(DocumentationType.SWAGGER_2).
select()
.apis(RequestHandlerSelectors.basePackage("com.company"))
.paths(PathSelectors.ant("/api/**"))
.build()
.apiInfo(apiInfo());
This is giving swagger base path as "basePath": "/"
I want to add base path as "basePath": "/api" so I followed diff threads like this How to change basePath for Springfox Swagger 2.0 and added
return new Docket(DocumentationType.SWAGGER_2).
select()
.apis(RequestHandlerSelectors.basePackage("com.company"))
.paths(PathSelectors.ant("/api/**"))
.build()
.apiInfo(apiInfo())
.pathProvider(new RelativePathProvider(servletContext) {
#Override
public String getApplicationBasePath() {
return "/api";
}
});
now the base path is changed to "basePath": "/api" and I updated my path mapping to #RequestMapping(path = "/resourceName/v1") as base has been added.
When I send the request from swagger, the request is going to /api/resourceName/v1 but the service returns 404.
When I send the request through postman for /resourceName.v1 then it works.
So the api is registred as /resourceName/v1 and the base is just added by swagger on top of it and will not work if the request sent throguh swagger UI
Then I added server.servlet-path=/api to application.properties to register basepath in the request mapping and now swagger shows the base path as /api without needing additional configuration.
But the problem is now swagger documentation is available at http://localhost:8080/api/swagger-ui.html instead of http://localhost:8080/swagger-ui.html. As we have all our other services doc at http://host/swagger-ui.html this is not useful.
Is there any way to add the base and still access the doc at http://host/swagger-ui.html and api's works as expected fro swagger and postman
Yes, you can add base path for all swagger requests. I used following config for this purpose:
#Configuration
#EnableSwagger2
public class SpringFoxConfig {
#Bean
public Docket api() {
Docket docket = new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
docket.pathMapping("api");
return docket;
}
}
Hope this helps.
There is way to change the api-doc base path using properties defined in application.properties file.
The properties are:
springfox.documentation.openApi.v3.path=/external/api/app/v3/api-docs
springfox.documentation.swaggerv2.path=/external/api/app/v2/api-docs
This can help you to change the path.

swagger-ui doesn't list any of the controllers endpoints

I'm trying to add Swagger to a very simple hello word Spring-Boot project.
I'm following this tutorial :
https://www.baeldung.com/swagger-2-documentation-for-spring-rest-api
this is my SwaggerConfig:
#Configuration
#EnableSwagger2
public class SwaggerConfig{
#Bean
public Docket greetingApi() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("com.example.swaggerready"))
.build()
.apiInfo(metaData());
}
private ApiInfo metaData() {
return new ApiInfoBuilder()
.title("Spring Boot REST API")
.description("\"Spring Boot REST API for greeting people\"")
.version("1.0.0")
.license("Apache License Version 2.0")
.licenseUrl("https://www.apache.org/licenses/LICENSE-2.0\"")
.build();
}
}
However, the results I have running it is only the first page without any information.
This is the repository if someone wants to see the full code.
https://github.com/ThadeuFerreira/SpringMicro
In class SwaggerConfig you need to change line:
.apis(RequestHandlerSelectors.basePackage("com.example.swaggerready"))
To:
.apis(RequestHandlerSelectors.basePackage("com.example.SpringMicro"))

SpringCloud Config Server: Path Mapping Conflict with SwaggerUI

I am running a SpringBoot config server (with Vault backend) and tried to add Springfox SwaggerUI to it. But since I do not want a prefix (spring.cloud.config.server.prefix) for my config server, the path mapping between the config server and SwaggerUI results in a conflict.
All my clients ask the config server with the following pattern:
{config-server-host}/{application-name}/{profile}
For example:
{config-server-host}/test-app-one/dev
{config-server-host}/test-app-two/prod
But my SwaggerUI path maps to:
{config-server-host}/swagger-ui.html
As a consequence, the config server complains that the app "swagger-ui" cannot be found or that no profile was specified.
This is the configuration of my Docket bean for the SwaggerUI:
#Configuration
#EnableSwagger2
public class SwaggerConfig {
#Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.paths(not(regex("/error.*")))
.paths(any())
.build()
.pathMapping("/");
}
private ApiInfo apiInfo() {
Contact contact = new Contact({secret}, {secret}, {secret});
return new ApiInfoBuilder().title({secret})
.description({secret})
.version({secret})
.contact({secret})
.build();
}
}
This all results in the following question: I cannot have a prefix for the config server and I want the SwaggerUI url mapped to the standard. Is there any possibility to tell the config server that it should exclude the /swagger* paths?
The spring.cloud.config.server.prefix parameter in the bootstrap.properties file fixed the issue for me.

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);
}
}

Is there a way I can stop springfox swagger from scanning the model classes?

I'm currently using Springfox Swagger to document my spring boot application with a Java config. My API starts in about 75 seconds, (it was originally 20 secs without Springfox) with the whole scanning process. I currently just need the controller information without any model info. Is there a way I can exclude model scanning from the startup process in order to make my API start faster? And are there any other ways to make it faster? I'm using swagger 1.2
There is a way to prevent Sprinfox framework from generating a Swagger model or parameter information of specified ignored types. You have to use the method ignoredParameterTypes in SwaggerSpringMvcPlugin or Docket class to let it know the types to be ignored.
Swagger 1 Example
Here is an example of Swagger 1 Java configuration with ignored types. It definitely had an impact on my application startup time.
#Configuration
#EnableSwagger
public class SwaggerConfiguration {
#Autowired
private SpringSwaggerConfig springSwaggerConfig;
#Bean
public SwaggerSpringMvcPlugin api() {
Class[] clazz = {MyClassA.class, MyClassB.class};
return new SwaggerSpringMvcPlugin(this.springSwaggerConfig)
.apiInfo(apiInfo())
...
.ignoredParameterTypes(clazz);
}
private ApiInfo apiInfo() {
...
}
}
Swagger 2 Example
Here is an example of Swagger 2 Java configuration with ignored types,
#Configuration
#EnableSwagger2
public class SwaggerConfiguration {
#Bean
public Docket api() {
Class[] clazz = {MyClassA.class, MyClassB.class};
return new Docket(DocumentationType.SWAGGER_2)
.groupName("my-group")
.select()
.paths(PathSelectors.any())
.build()
.apiInfo(apiInfo())
.ignoredParameterTypes(clazz);
}
private ApiInfo apiInfo() {
...
}
}
With swagger 3.0 #Hidden annotation is available which is present at package io.swagger.v3.oas.annotations
It can be used on top of the class or method to exclude the resource in swagger documentation.
Springfox Swagger2 acquire UI data through GET /v2/api-docs, which will mapping to springfox.documentation.swagger2.web.Swagger2Controller.getDocumentation().So you can just create a bean to take place of 'ServiceModelToSwagger2Mapper' with your sanning logic:
#Primary
#Component
class CustomServiceModelToSwagger2Mapper : ServiceModelToSwagger2MapperImpl() {
override fun mapDocumentation(from: Documentation?): Swagger? {
// scanning logics...
}
}
refer to my another related answer : https://stackoverflow.com/a/64057512/14332259

Resources