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

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"))

Related

SpringFox Swagger UI has wrong base url

I got confused with spring fox swagger ui base url, they are not pointing to correct url.
I just deployed a war in a context, so the app is in 127.0.0.1:8080/bff, i managed to add swagger and success, now its running in 127.0.0.1:8080/bff/swagger-ui.html, but when i try to test the api its pointing to 127.0.0.1:8080/bff/v2/api-docs/api/v1/home/profile. Why there is v2/api-docs !?
I know the API list on the swagger-ui is populated from that one, but why it's injected to URL when we test the API? because all of my API lay on the 127.0.0.1:8080/bff/api/v1
This is the screenshot
This is the code.
#Configuration
#EnableSwagger2
public class SwaggerConfig {
#Autowired
private GitVersionPropertiesConfig gitVersionPropertiesConfig;
#Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.globalOperationParameters(
Lists.newArrayList(new ParameterBuilder()
.name("Authorization")
.description("OAUTH2 Token")
.modelRef(new ModelRef("string"))
.parameterType("header")
.required(false)
.build()))
.apiInfo(apiInfo())
.pathMapping("/")
.pathProvider(new RelativePathProvider(null) {
#Override
public String getApplicationBasePath() {
return "/bff/";
}
})
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.regex("/api.*"))
.build();
}
ApiInfo apiInfo() {
String desc = "Bima Friends Forever API<br>"
+ "Current Branch : <b>"+gitVersionPropertiesConfig.getGitBranch()+"</b><br>"
+ "Timestamp : <b>"+gitVersionPropertiesConfig.getGitBuildTime()+"</b>";
return new ApiInfoBuilder()
.title("BFF - Hutchison")
.description(desc)
.version(gitVersionPropertiesConfig.getGitCommitIdAbbrev())
.build();
}
}
This is the temporary fix, but not permanent.
Open browser console and run window.swaggerUi.api.setBasePath('/bff');
Server : Wildfly
Swagger UI Version : 2.7.0
Thanks in advance.
I manage to fix it.. the culprit was jboss-web.xml context
Previously
<jboss-web>
<context-root>/bff/</context-root>
</jboss-web>
Fix :
<jboss-web>
<context-root>/bff</context-root>
</jboss-web>
oh my god...

Spring Boot + Swagger-Ui yml generate

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.

Support multiple pathmapping in Swagger UI/Spring boot

I am using swagger 2.0 in a Spring boot(version 1.5.9.RELEASE) project.
Swagger works fine but now documentation have hundreds of api and I want to redirect documentation on different different urls.I am having swagger configuration like blow.
#Configuration
#EnableSwagger2
public class SwaggerConfig {
#Bean
public Docket postsApi() {
return new Docket(DocumentationType.SWAGGER_2).groupName("public-api")
.apiInfo(apiInfo()).select().paths(postPaths()).build();
}
private Predicate<String> postPaths() {
return or(regex("/api/posts.*"), or(regex("/api/.*"), regex("/secure/api/.*")));
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder().title("Swagger API")
.description("Swagger Integration with Spring Boot")
.termsOfServiceUrl(null)
.license(null)
.licenseUrl(null).version("1.0").build();
}
}
Please suggest any way. Thanks in advance.
Finally I break these api's into groups basis on their url as following code segment, creates three group one for Settings, another for Products and last one contains all the other documentation except settings and products.
#Bean
public Docket swaggerSettingsApi() {
return new Docket(DocumentationType.SWAGGER_2)
.groupName("Settings")
.select()
.apis(RequestHandlerSelectors.basePackage("com.xyz"))
.paths(regex("/secure/api/v1/settings/.*"))
.build()
.apiInfo(new ApiInfoBuilder().version("1.0").title("Settings API").build())
.globalOperationParameters(operationParameters());
}
#Bean
public Docket swaggerProductApi() {
return new Docket(DocumentationType.SWAGGER_2)
.groupName("Product")
.select()
.apis(RequestHandlerSelectors.basePackage("com.xyz.modules.v1"))
.paths(productPaths())
.build()
.apiInfo(new ApiInfoBuilder().version("1.0").title("Product API").build())
.globalOperationParameters(operationParameters());
}
#Bean
public Docket swaggerModuleApi() {
return new Docket(DocumentationType.SWAGGER_2)
.groupName("Others")
.select()
.apis(RequestHandlerSelectors.basePackage("com.xyz.modules.v1"))
.paths(postPaths())
.build()
.apiInfo(new ApiInfoBuilder().version("1.0").title("Other Modules API").build())
.globalOperationParameters(operationParameters());
}
private Predicate<String> postPaths() {
return or(regex("^(?=\\/secure\\/api\\/v1\\/)(?!.*(settings|products)).+\\/.*"));
}

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