I'm using spring boot + swagger 2 for documenting all the REST API .I'm able to list all the api of a controller when i have the below project structure.
If i move the swaggerconfig.java to the config package then i'm not able to list all api of a controller.i'm getting
This is my SwaggerConfig.java
#Configuration
#EnableAutoConfiguration
//#ComponentScan(basePackages="com.javainuse.swaggertest")
#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.*"), regex("/api/javainuse.*"));
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder().title("JavaInUse API")
.description("JavaInUse API reference for developers")
.termsOfServiceUrl("http://javainuse.com")
.contact("javainuse#gmail.com").license("JavaInUse License")
.licenseUrl("javainuse#gmail.com").version("1.0").build();
}
}
What i'm doing wrong
I fixed the problem. The problem is browser cache. I just cleared all the cache, and then I'm able to get the swagger-ui.html with all api list.
what worked for me is:
#Bean
public Docket customImplementation(){
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("your base package"))
.paths(PathSelectors.any())
.build();
notice paths is not specific, its any.
#Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
.apis(Predicates.not(RequestHandlerSelectors.basePackage("org.springframework.boot")))
.apis(Predicates.not(RequestHandlerSelectors.basePackage("org.springframework.cloud")))
.apis(Predicates.not(RequestHandlerSelectors.basePackage("org.springframework.security")))
.build();
}
Although the OP issue was solved already, I stumbled upon this thread and my solution was somewhat different. Here it is for others should they find themselves in the same position:
I had the same issue, but with using swagger-codegen-maven-plugin (v3)
The API is defined in a api.yml specification file and I generate the interface and models with the plugin.
It has an option to also generate a controller, in which case it was showing in the UI. However when I configure the controllers to be ignored/skipped for generation and just implement my own it was not showing.
The problem was the package. The plugin also auto generates the SwaggerDocumentationConfig class which includes all controllers from the API package that is configured. My own controllers had to be in the exact same package.
Code snippets to clarify:
In my pom.xml I have configured the plugin with:
<apiPackage>a.b.c.spec.api</apiPackage>
The auto generated SwaggerDocumentationConfig class contains:
#Bean
public Docket customImplementation() {
return (new Docket(DocumentationType.SWAGGER_2)).select()
.apis(RequestHandlerSelectors
.basePackage("a.b.c.spec.api"))
.build().apiInfo(this.apiInfo());
}
My controller lives in the package a.b.c.api so I had to change the apiPackage property to align and the issue was resolved.
This solved my issue.
Add .pathMapping("/") in the Docket
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build()
.pathMapping("/");
I am working on maven based Java Spring Boot project.
In my case, it was a version mismatch of swagger and swagger-ui dependency in pom.xml
In my scenario, it's because of the #Profile("dev") configuration in SwaggerConfig limited for dev profile only, while I was running with another profile.
Checking versions of Swagger-UI and springfox-swagger2 in pox.xml
Adding #EnableSwagger2 in application.java helped.
Related
I've read many articles/threads and what not about how to enable Jackson's WRAP_ROOT_VALUE feature in SpringBoot (v2.2.2RELEASE) and none of them really work, until I came across the following solution which does the trick!
#Configuration
public class JacksonConfig
#Bean
#Primary
public Jackson2ObjectMapperBuilder jacksonBuilder() {
Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder() //
.featuresToEnable(SerializationFeature.WRAP_ROOT_VALUE); // enables wrapping
return builder;
}
This works since it replaces SpringBoot/Swagger's serialization definitions with this custom one.
The problem: Swagger3 stop working! I access swagger using this link:
http://localhost:8080/swagger-ui/index.html and it stopped working! If I remove the JacksonConfig then everything is back to normal.
My assumption is that Swagger initializes Jackson in some way that ruins my custom one.
Any idea?
I'm running a SpringBoot (2.2.2.RELEASE) java application with springfox.boot.starter:3.0.0 and when I run it locally from within my Eclipse IDE it works well. The problem is that when I package it as Docker image and run the container then when I try to run http://localhost:8089/swagger-ui/# it displays the default Swagger Petstore example.
Here is my Docket #Bean:
#Configuration
public class SwaggerConfig {
#Bean
public Docket api() { //
return new Docket(DocumentationType.OAS_30) //
.apiInfo(DEFAULT_API_INFO) //
.select() //
.paths(PathSelectors.regex("/error").negate()) //
.build();
}
}
Obviously it's something related to running it from within a docker container, any idea what's missing to make it work?
Thank you!
EDIT:
Probably worth mentioning that I use to work with springfox:2.9.2 and it worked well both locally and as Docker container.
I think u must use http://localhost:8089/swagger-ui.html instead of http://localhost:8089/swagger-ui/#, this might work.
I have two different url patterns one which starts with /api and second which starts with /svc. So, my question here is can we have two different swagger endpoint urls for these both API documentation using Swagger ?
I am using Spring MVC and Swagger for documentation purpose. Any hint please ? Thanks in advance!
#Configuration
#EnableSwagger2
public class SwaggerConfig {
#Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}
}
This has to help you.
The code:
apis(RequestHandlerSelectors.any())
will select all the apis from your controllers.
As the title says, is it possible to include a POJO in a swagger doc if it's not used in a controller method?
I've tried using the #ApiModel annotation on the POJO class, i.e.:
#ApiModel("POJO")
public class Pojo {
...
}
However, unless the POJO is returned by a controller, I haven't been able to have it appear in the generated swagger docs. Is there a way to accomplish this?
I'm using springfox version 2.9.2, by the way.
It is possible with Springfox. You just have to modify your Docket. Add additionalModels method to your Docket implementation:
#Autowired
private TypeResolver resolver;
#Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build()
...
.additionalModels(resolver.resolve(Pojo.class));
}
The CustomProperties object is in an external library, so I cannot add the annotation, but I still would like to feed it from my Spring Boot YAML by means of a prefix.
Found it!
In the #Configuration:
#Bean
#ConfigurationProperties("company.custom")
public CustomProperties customProperties(){
return new CustomProperties();
}