How to configure Swagger with Openapi - spring-boot

I just started using springdoc-openapi. I want to be able to customize the Swagger for things like background color, logo, etc. Is there a way to do that? I see that springdoc-openapi-ui includes webjars/swagger-ui, but I'd hate to just run a customize version. Would prefer to do it as an update so it doesn't interfere with future upgrades
Just to experiment, I've tried copying the entire swagger-ui distribution to my resources directory: resources/swagger-ui. I've also tried resources/webjars/swagger-ui.
In my pom, I have
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>1.6.6</version>
<exclusions>
<exclusion>
<groupId>org.webjars</groupId>
<artifactId>swagger-ui</artifactId>
</exclusion>
</exclusions>
</dependency>
So it should only use my local version. But I get a 404
GET "/swagger-ui/index.html", parameters={}
Mapped to ResourceHttpRequestHandler [classpath [META-INF/resources/webjars/]]
Resource not found
Completed 404 NOT_FOUND
Not sure why it's not finding it at swagger-ui/index.html

It looks like you need to set some of the variables in the .properties file:
try
springdoc.swagger-ui.path=/swagger <- sets the url path so you can see localhost:8080/swagger/index.html
springdoc.swagger-ui.enabled=true <- sets the swagger-ui to enabled
springdoc.api-docs.enabled=true <- sets the springdoc openapi enpoint to enabled
I would look here for other property setting you might need - such as turning off swagger in production, etc...
https://springdoc.org/#properties
As for customization, you will have to create your own .html page and disable the use of the default one.
springdoc.swagger-ui.path=MyNewHtmlFile.html
I think is the property name for setting your own .html file.

Related

Stop sending Spring boot metrics to Prometheus with Micrometer

I have a Spring boot application where I am sending custom metrics generated within the service to Prometheus via Pushgateway.
I am using Prometheus Pushgateway with Micrometer, mainly based on this tutorial: https://luramarchanjo.tech/2020/01/05/spring-boot-2.2-and-prometheus-pushgateway-with-micrometer.html
I have following dependencies in my pom.xml
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-core</artifactId>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient_pushgateway</artifactId>
<version>0.16.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
And sending custom metrics with:
Counter counter = Counter.builder("sb_console_test_counter").register(meterRegistry);
counter.increment();
It is working fine and I can view the custom metrics generated by the application however in addition to this I am seeing application specific metrics generated by Spring boot e.g.
tomcat_sessions_active_current_sessions
tomcat_sessions_active_max_sessions
etc.
I only want to capture the custom metrics generated by my code and not any other generic metrics, how can I stop sending this?
When you add the dependency spring-boot-starter-actuator you will get a lot of metrics out of the box from various configurations such as JvmMetricsAutoConfiguration and TomcatMetricsAutoConfiguration.
To filter those out, you could add a deny filter in your Micrometer config, and only allow your custom metric meters to be registered.
Example using a deny filter:
#Bean
public MeterRegistryCustomizer<MeterRegistry> metricsRegistryConfig() {
return registry -> registry.config()
.meterFilter(MeterFilter.deny(id -> !id.getName().startsWith("sb_console")));
}
The above will deny any metrics not starting with sb_console.
See this link for more info about the meter filters
I believe you can just add this:
management.metrics.export.defaults.enabled=false
to your application.properties. I see this in some of my code. I'm not set up to try it right now, but that's why we have it there.
If you want to add back some of the built in metrics, you can then add lines like this:
management.metrics.export.<groupname>.enabled=true

springdoc-openapi doesnt pick up changes in controller classes

I have a 3-module Springboot multi-module maven project with a structure like so:
parent
|- module 1
|- module 2
|- module 3
The maven dependency for spring-openapi is in the parent pom, while the main class is in module 1.
The problem is that when I examine the Swagger api, I only see the documentation for the rest endpoints from module 1, and not those in 2 or 3.
I experimented by putting the openapi dependency in module 3 and re-building but that doesn't make a difference. Is it not possible to get all the rest-endpoints across all modules published all at once?
Some more info: here are some properties from my application.properties:
springdoc.packagesToScan=com.sxxxxxxxxx
springdoc.pathsToMatch=/a/**, /d/**
These are my dependencies:
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>1.6.9</version>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-security</artifactId>
<version>1.6.9</version>
</dependency>
I thought that this Q&A might help (but it didnt): OpenAPI & spring-doc not finding all mappings in a controller class
I've been doing some experimentation:
I added a Test Controller, and it appeared in the docs. Then I deleted the Test Controller from the code base, but it still appears in the docs
I've changed the http response codes to some of the endpoints in the documentation, but those aren't picked up.
It almost seems like once it does the initial generation of documentation for a rest controller, it forgets about looking at it again for any further changes.
Does any one have an idea?
Thanks

WebClient create java.lang.ExceptionInInitializerError: null ExchangeStrategies.withDefaults

I'm trying to play around Spring WebFlux Webclient.
Tried to create a simple webclient in a quartz cron job
this.webClient = WebClient.create(this.queryUrl);
I made sure the queryUrl is valid.
However, I've got an error as following:
java.lang.ExceptionInInitializerError: null
at org.springframework.web.reactive.function.client.ExchangeStrategies.withDefaults(ExchangeStrategies.java:67)
at org.springframework.web.reactive.function.client.DefaultWebClientBuilder.initExchangeStrategies(DefaultWebClientBuilder.java:302)
at org.springframework.web.reactive.function.client.DefaultWebClientBuilder.build(DefaultWebClientBuilder.java:269)
at org.springframework.web.reactive.function.client.WebClient.create(WebClient.java:154)
Tracing into the code, I've found that
DefaultExchangeStrategiesBuilder is null.
Does anyone have any idea what is missing?
It's a simple web client.
Thanks.
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.13.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.13.0</version>
</dependency>
Adding these dependencies and making sure the versions are the same.
I only had jackson-core before.
I tried to move the job out of quartz and ran it directly in main.
it still threw an exception, but it's different and I then looked it up
and some one has asked the issue and the solution to make sure both jackson packages need to be included and versions must be same.
I then added them to the pom file. try to run the job and it worked.
Putting the job back to quartz to run and it also worked.

Unable to access /actuator endpoints

I've got a small project running Spring Boot and Spring Cloud Streams with version Greenwich.SR4.
So far I've tried to include the following dependency.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-actuator-autoconfigure</artifactId>
</dependency>
Wanting to have a /check/health endpoint which basically returns a code 200.
I've modified the config accordingly so that I don't have useless health configs within.
management.endpoints.web.base-path=/check
management.endpoint.health.enabled=true
management.health.solr.enabled=false
management.health.elasticsearch.enabled=false
However, whenever I try to run localhost:8080/check/health a 404 is prompted. What could be happening?
Also mentioning that I've tried including my own #RestController but same behavior happens.
The URL for the health check should be http://localhost:8080/check/health, not /check/status.
It is depends on the Spring-Boot version. After version 2.0 the path name is manage, so your right url is: http://localhost:8080/check/health
You can also change that with (.yml version):
management:
context-path: /manage
security:
enabled: false

Spring basic security confiugred userid/password not working

Our application is based on Spring-boot 2.0. I've enabled basic security by adding the following dependency to pom.xml:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
<version>2.0.4.RELEASE</version>
</dependency>
I also have added properties so that I can define my own userid and password for basic security, instead of the generated ones. I defined them like this in /resources/applicaiton.properties file:
security.user.name=user1
security.user.password=pass1
When I startup my application, I can see that is still generates the password for me in the log. Also, I am unable to login using user1/pass1 combination. I can only successfully login with the user=user and password=generated-password-from-log file.
Why won't spring security allow me to login with user1/pass1? What could be the problem?
Those properties need the spring prefix.
spring.security.user.name=user # Default user name.
spring.security.user.password= # Password for the default user name.
If I want to configure something I often take a look at this List
I hope this helps.

Resources