Spring Integration with swagger: How can I implement it? - spring

Cant expose spring integration flow apis, with swagger documentation
I have some apis exposed using spring integration. We tried document it, with springfox dependency (swagger2).
But when access to: http://localhost:8080/myProject/swagger-ui.html, the page is empty, we cant see the services with swagger format
My example;
Class definition:
#Configuration
#EnableSwagger2
public class ConsultaBdnFlow {
....
}
Swagger configuration:
#Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}
Swagger Dependencies:
<!-- Start Swagger 2 with SpringFox -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.3.0</version>
</dependency>
<!-- End Swagger 2 with SpringFox -->
Flow to expose the service:
#Bean
public IntegrationFlow bdnBlacklistFlow() {
return IntegrationFlows
.from(Http.inboundGateway("/consultas/bdn")
.requestPayloadType(String.class)
.requestChannel(requestBlacklistChannel())
.replyChannel(replyBlacklistChannel())
)
.get();
}
When run the project we can access to http://localhost:8080/swagger-ui.html, but dont see the service swagger document

Maybe you are missing VendorExtension.
Change your Docket Bean to something like this and see what happens. Also include the errors it is giving you if it doesn't work. This worked on Swagger dependencies (2.9.2) and Spring-boot 2.2.0.M2.
#Bean
public Docket apiDocket() {
Contact contact = new Contact(
"You name",
"Your webesite",
"Your email"
);
List<VendorExtension> vendorExtensions = new ArrayList<>();
ApiInfo apiInfo = new ApiInfo(
"RESTful API documentation",
"This pages documents Turing Ecommerce RESTful API endpoints", "1.0",
"Website", contact,
"Apache 2.0", "http://www.apache.org/licenses/LICENSE-2.0",vendorExtensions);
Docket docket = new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo)
.select()
.apis(RequestHandlerSelectors.basePackage("com.company.package"))
.paths(PathSelectors.any())
.build();
return docket;
}

Related

How do I hide endpoints only for a specific profile in spring boot?

I am using open api 3 and want to hide some endpoints in swagger ui. In swagger2 I found what can be done in this way by creating my own annotation, but I don't understand how I can do it in openapi3.
#Bean
public Docket postsApi() {
return new Docket(DocumentationType.SWAGGER_2)
.groupName("public-api")
.apiInfo(apiInfo())
.select()
// This is the part that will ignore the method
.apis((handler) -> !handler.isAnnotatedWith(IgnoreForProd.class))
.build();
}
I've solved this issue like this:
1st - I have removed the springfox and springfox-swagger-ui dependencies from pom.xml and replaced it with springdoc-openapi-ui like this:
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>1.6.6</version>
2nd - created the Bean below:
#Configuration
public class OpenAPIConfig {
#Value("${version:unknown}")
private String version;
#Bean
public OpenAPI springShopOpenAPI() {
return new OpenAPI()
.info(new Info().title("Title API")
.description("Description API")
.version(version)
.license(new License().name("Apache 2.0").url("http://springdoc.org")))
.externalDocs(new ExternalDocumentation()
.description("SpringShop Wiki Documentation")
.url("https://springshop.wiki.github.org/docs"));
}
}
3rd - then on application.yml you can filter by package and/or path:
springdoc:
packages-to-scan: com.company.projectxyz.controller
pathsToMatch: /api/whatever/public/**
4th - and if you need to filter different paths/packages per profile, also on application.yml you can do the 3rd step inside of:
spring.config.activate.on-profile: dev/qa/prod...
and access your swagger at /swagger-ui/index.html
I hope it helps =)

Swagger 3 with Springboot: Unable to infer base url

I use Springboot with swagger 3:
<!-- SWAGGER -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
I use a default /api prefix to all my endpoints.
This is how I configured my SwaggerConfig:
#Configuration
#EnableSwagger2
public class SwaggerConfig {
public static final String AUTHORIZATION_HEADER = "Authorization";
#Bean
public Docket api() {
Docket docket = new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.securityContexts(Collections.singletonList(securityContext()))
.securitySchemes(Collections.singletonList(apiKey()))
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build().pathMapping("/api");
return docket;
}
private ApiKey apiKey() {
return new ApiKey("JWT", AUTHORIZATION_HEADER, "header");
}
// ......
}
When I try to access to my swagger-ui http://myhost/swagger-ui I get a popup with this message Unable to infer base url. This is common when using dynamic servlet registration or when the API is behind an API Gateway. The base url is the root of where all the swagger resources are served. For e.g. if the api is available at http://example.org/api/v2/api-docs then the base url is http://example.org/api/. Please enter the location manually: asking me to define the location with.
When I enter my prefix manually : http://myhost/api then every thing is fine.
Any idea how to define my REST API prefix ?

generating spring API docs using swagger over GET endpoints with complex objects

I have a spring boot app and I am using springfox swagger to generate the API documentation.
I have a search endpoint with a complex, nested object
#GetMapping("/search")
public Something search(SearchDTO input) {
}
public class SearchDTO {
private SearchFilterDto filters;
private Page page;
private Sort sort;
}
public class SearchFilterDto {
private String name;
}
... other DTOS; getters and setters are omitted, default constructor
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
After running the app, the generated doc looks like this
Any ideas on to can I help springfox generate documentation for my complex object?
I have found the problem.
In my DTOs I had some getters which return Optional<Something>. By adding .genericModelSubstitutes(Optional.class) to my swagger config I managed to obtain the configuration I was looking for.
#Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build()
.genericModelSubstitutes(Optional.class) // added this line
.securityContexts(Lists.newArrayList(securityContext()))
.securitySchemes(Lists.newArrayList(apiKey()));
}

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