Swagger on Spring Boot Actuator - spring-boot

I'm documenting my API with Swagger and I'm applying the following annotation to all of my REST Controllers:
#Api(value = "Some value", description = "Some Description", tags = {"Tag Tag Tag"})
However, I also want to include the endpoints provided by Spring Boot Actuator (/health, /info) on Swagger (and they are being included), however I can't seem to find a way to change their default description, tags, and title, among other swagger properties.
Is there a way to do this? Thanks in advance.

Related

How to change the Span Name for a REST Endpoint with Spring Cloud Sleuth

I am using Spring Cloud Sleuth to create traces.
I am exposing a REST endpoint using Spring Boot like this:
#SpanName("calculate-reviews")
#GetMapping(path = "/reviews/{productId}")
public Reviews bookReviewsById(#PathVariable String productId) {
...
}
My expectation would be that the span that is created by Spring Cloud Sleuth is named calculate-reviews, however, that's not the case. Instead the default span name is generated which looks like this: get /reviews/{productId}
Is there a chance to change the span name for a REST endpoint at all? How would I achieve this?
I'm not sure it is a good idea to modify it. It gives you additional information that could be valuable if you don't know the service that you are calling by heart (most of the cases).
You can modify the Span using a SpanHandler: see the docs or inject the tracer, get the current span and change the name.

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

Display default value of a model attribute in Swagger UI

I am using Spring Boot Java and Swagger 2 to document my APIs.
What Spring annotation can I use to show the default value mydoggie in Swagger UI?
You can add an example and a default value to show in Swagger 2 with this annotation:
#ApiModelProperty(value = "value to show", example = "example to show")
I hope it helps

Use of "reference" element in #ApiResponse annotation in swagger to refer response samples

I need to show the response example value with respect to each code in Swagger UI, I'm using Spring Boot + springfox-swagger2
It seems that the "reference" attribute is not been read by during the generation of the swagger file (see https://github.com/springfox/springfox/issues/1705)

generate sample example in swagger UI (in Spring boot project)

I am using a spring boot application and have configured using Swagger UI.
I want to know whether we could pre-populate the example value with sample value so we can hit the "Try it out!" button without having to type in some sample values to get a response.
It must be present there.
Is there a way we can do this using annotations or a separate file which Swagger uses?
I am using a spring boot project with springfox-swagger2:2.7.0 and springfox-swagger-ui:2.7.0 with dependencies added using gradle.
Since the #ApiParam properties example and examples are not working (see this issue on GitHub), support for adding examples is limited/non existing.
What you can do for simple parameters (#RequestParam) is to add the #ApiParam annotation with the defaultValue property, like this:
#GetMapping
public List<Foo> findAll(
#RequestParam(required = false)
#ApiParam(defaultValue = "foo") // Put the default value here
String input) {
// ...
}
However, there is no support yet for doing this with #RequestBody parameters.
A possible workaround for #RequestBody parameters is by clicking on the code box at the right side of the Swagger tester, where it says Example value. If you click on it, it will insert that example into the field itself.
Here is a workaround for providing an example:
Inject html into swagger
#ApiParam(
name="whatever",
value="whatever",
defaultValue="none</p><p>Example: xyz</p>"
)
They don't protect against it in the latest version 2.9.2.

Resources