Springdoc Swagger 3 - Failed to load API definition after adding API Group - spring-boot

I have a Spring boot application that works perfectly fine with Springdoc until I add the following code to group the endpoints into a default group.
#Bean
public GroupedOpenApi hideApis() {
return GroupedOpenApi.builder().group("default")
.pathsToExclude("/api/v2/**", "/v2/**")
.pathsToMatch("/api/v1/**", "/v1/**")
.addOperationCustomizer(new customCustomizer())
.build();
}
As soon as this part of code is added I'm getting the below error
Any idea what is wrong?
Springdoc v1.4.3
Spring-boot 2.x

Cause by a Null Pointer Exception in the OperationCustomizer class, i.e in the customCustomizer() of the given code.

Related

Spring-Doc open api not working with Spring cloud config server #EnableConfigServer

Im using spring boot 2.3.2.RELEASE with spring-cloud-config-server 2.2.4.RELEASE. Im trying to implement the spring-doc-openapi (1.4.3) in a existing project. If i add #EnableConfigServer in one the configuration class file, the swagger-ui.html endpoint returns a weird json:
"name":"swagger-ui",
"profiles":[
"index.html"
],
"label":null,
"version":null,
"state":null,
"propertySources":[
]
}
and not the the swagger ui as expected. Im not sure if its a bug, but would appreciate any kind of help.
Not sure if its relevant to add the springdoc dependency on spring cloud config server, unless you need to explore some APIs on the config server it self.
Here is the link of a fully working example using springdoc with config server:
https://github.com/springdoc/springdoc-openapi-demos/tree/master/sample-springdoc-openapi-microservices
And this is the link of a blog which explains the natural usage with microservies and spring cloud modules:
https://piotrminkowski.com/2020/02/20/microservices-api-documentation-with-springdoc-openapi/
Answer from #brianbro seems not to be working anymore...
Verified on: springdoc-openapi v1.6.6 and org.springframework.cloud:spring-cloud-config-server:v2.2.4.RELEASE
Here is how I solved it:
List item spring.cloud.config.server.prefix=config-server - please note that any request to config server will require to add prefix!
Add following bean (sample implementation in Kotlin)
#Bean fun configServerApi(): GroupedOpenApi =
GroupedOpenApi.builder()
.group("Config server")
.pathsToMatch(
"/config-server/**"
)
.build()
Now you should be able to reach swagger ui :)

Spring Boot 2 controller returns a callable but http response remains empty

I'm in the process of migrating a Spring Boot 1.5 project to Spring Boot 2.1.8 (JDK 1.8, Kotlin 1.3.50).
I have a few controllers whose methods look like:
#PostMapping("post")
fun post(#RequestBody input: String): Callable<JsonNode> {
return Callable {
requestAsJson(input)
}
}
This works well in Spring Boot 1.5, without any further configuration. However with Spring Boot 2.1.8, the call does not fail but the HTTP response remains empty.
When I use start.spring.io to generate a minimalistic example, it works fine, so I guess that there is something wrong in my configuration.
When I enable debug traces for the Spring MVC, the final trace I get is:
[nio-8080-exec-3] m.m.a.RequestResponseBodyMethodProcessor : Writing [{"data":{"...
[nio-8080-exec-3] o.s.web.servlet.DispatcherServlet : Exiting from "ASYNC" dispatch, status 200
So this looks fine to me, but still no response is received (using Curl or Postman to test).
I'm a bit at a loss now, since it was working like a charm in Spring Boot 1.5 and I'm trying to get a hint on how to get out of this issue.
Thanks for any help,
Damien
I had declared a ShallowEtagHeaderFilter bean the following way in my WebMvcConfigurer:
#Bean
fun shallowEtagHeaderFilter(): ShallowEtagHeaderFilter {
return ShallowEtagHeaderFilter()
}
While this was working fine in Spring Boot 1.5, this causes the controller async methods (returning a Callable) to not return any content any longer. Removing this bean for Spring Boot 2.1 restores a normal behavior.
I still need to seek a way to have my e-tag, but for now, this solves the present issue.

Spring Boot in Cloud Foundry (PCF) disable Whitelabel Error Page

Related question:
Spring Boot Remove Whitelabel Error Page
For my case,
I disabled whitelabel by setting whitelabel.enabled = false, and I also exclude ErrorMvcAutoConfiguration. It worked in regular spring boot service. But I deployed the same service on PCF cloud foundry, then spring still want to redirect error to /error page.
Any help and suggestion is welcome.
Edit:
I added exclude annotation on Application, then it works on PCF.
Previously I added exclude configuration in application.yml, then it didn't work on PCF
You need to create a separate endpoint /error and then handle it in the method. I would suggest you to maintain a separate controller infact. My code would look something like this
#RestController
#RequestMapping(path = "/error")
public class ErrorController {
#ApiOperation(value = "Error Page", notes = "Error Page")
#GetMapping
public String error() {
return "Some Error Occurred and I will Graciously show the Error"
}
}
It turns out circuit breaker service set exclusion property first, than local application.yml didn't take effect. If I add exclusion property in repo, then it take preference.
I feel this is kind of spring bug, since exclusion is list, it should include all items, instead of taking the first configuration only.

Swagger Path Selector Not Working

We are in a process of implementing Swagger for our REST APIs in a Spring Boot Application. Along with adding YAML files, we have also made changes to Spring Boot configurations to include a reg-ex pattern.
#Bean("myApi")
public Docket myApi() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(myApiPaths())
.build()
.pathMapping("/");
}
private Predicate<String> myApiPaths() {
return PathSelectors.regex("/v1/my/path/.*");
}
Currently all such YAML files are included in one library and are always available for all spring boot components. Because of this swagger-ui shows up all end points defined. This is why we were relying on path selectors so that only those matching patterns will be exposed.
But above code does not seem to be working. Despite trying different regex patterns, RequestHandlers I am still seeing the APIs which are not implemented on given Spring Boot Component.
Any help here will be appreciated.

How to configure Spring Boot with elasticsearch 5.2.1?

I am trying to connect my Spring Boot application to local elasticsearch 5.2.1 instance. When i use "org.springframework.boot:spring-boot-starter-data-elasticsearch" dependency, i face with "Received message from unsupported version: [2.0.0] minimal compatible version is: [5.0.0]". I think this is due to elasticsearch version is 2.4.4 in starter dependency. So to solve this error, i edit pom.xml properties by adding elasticsearch.version>5.2.1/elasticsearch.version> line. But this time i get
"java.lang.NoSuchMethodError: org.elasticsearch.client.transport.TransportClient.builder()"
To overcome this issue, i create custom config class like below:
#Configuration
public class ElasticsearchConfiguration {
#Bean
public Client client() throws UnknownHostException {
TransportClient client = new PreBuiltTransportClient(Settings.EMPTY)
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300));
return client;
}
#Bean
public ElasticsearchTemplate elasticsearchTemplate() throws UnknownHostException {
return new ElasticsearchTemplate(client());
}
}
This time i get apache.logging.log4j exceptions (check here) so i add necessary dependencies.
Finally i get below error and stucked there. Could anyone help me out with this?
nested exception is java.lang.NoClassDefFoundError:org/elasticsearch/action/count/CountRequestBuilder
The github page of spring-data-elasticsearch shows that it currently supports elasticsearch only up to version 2.4.0.
For now you have 3 options:
Wait and hope
checkout the pull request
checkout the dev branch 5.0.x-prep
You need to use Spring Boot 2. Check out my spring-boot-elasticsearch-5.x example.
You can use elasticsearch java api to create transport client instead of using spring-data-elasticsearch.
I tried the same and getting that error too for CountRequestBuilder, reason is that CountRequestBuilder class is deprecated and removed now from 5.x elastic search versions, that class is replaced by SearchRequestBuilder but unfortunately spring-data-elasticsearch don't provide this even in the latest release of its jar and that CountRequestBuilder is used in ElasticSearchTemplate.
I am also looking out for some solution. I will post if able to resolve.

Resources