spring boot - springdoc open api: No api definiation provided error - java

I need to document my spring boot application's rest apis with SpringDoc OpenApi. So, I added this dependency in my pom.xml:
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>1.6.8</version>
</dependency>
And here is my config class:
#Configuration
public class SwaggerConfig {
#Bean
public GroupedOpenApi apis() {
return GroupedOpenApi.builder()
.group("my-application")
.packagesToScan("com.myapp.base")
.pathsToMatch("/**")
.build();
}
}
When I want to use swagger-ui in /base-url/swagger-ui/index.html, the result is as below image:
What's wrong in my configuration?
I use spring boot 2.6.6.
Note: I don't see any problem to load swagger resources in browser console(401, 404). Also I have disabled spring security in app, there is not any problem related to security or loading swagger resources.

You bean looks okay to me but you're probably not hitting the right endpoint for the Swagger-UI.
The URL /base-url/swagger-ui/index.html points to the Swagger-UI provided by the Swagger Core library and there's not a lot you can do about it.
Springdoc has a transitive dependency on Swagger Core and as such the page is available. But the URL at which Springdoc exposes it's Swagger-UI is /base-url/swagger-ui.html.
Refer to the answer here for an explanation on why is it so and the ways to update it.

Related

Spring boot project add login endpoint to swagger API Doc (springdoc-openapi-ui)

I'm using java 17 and spring 2.6.3 and this package : springdoc-openapi-ui:1.6.5
and I was wondering how I can add login endpoint to the swagger documentation ?
I'm using Spring's security package and login endpoint is provided for me but still I want it to be included in my Swagger UI.

Don't spring-boot-starter-web and spring-boot-starter-webflux work together?

When I start to learn the spring-webflux, I have the question about this component.
I built a simple project, using maven to manage it. I addded the dependencies related to spring-boot-starter-web and spring-boot-starter-webflux, like :
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
But it doesn't work. When removing the spring-boot-starter-web dependency, it can work well.
As explained in the Spring Boot reference documentation section about the web environment, adding both web and webflux starters will configure a Spring MVC web application.
This is behaving like that, because many existing Spring Boot web applications (using MVC) will depend on the webflux starter to use the WebClient. Spring MVC partially support reactive return types, so this is an expected use case. The opposite isn't really true, since a reactive application is not really likely to use Spring MVC bits.
So using both web and webflux starters is supported, but it will configure a Spring MVC application. You can always force the Spring Boot application to be reactive with:
SpringApplication.setWebApplicationType(WebApplicationType.REACTIVE)
But it's still a good idea to clean up dependencies as it would be easy to use a blocking feature in your reactive web application.
I had a similar issue using spring-boot-starter-webflux and spring-data-geode causing
DEBUG [http-nio-8082-exec-2] org.sprin.web.servl.resou.ResourceHttpRequestHandler 454 handleRequest: Resource not found
It was resolved by changing the application type
#SpringBootApplication
public class Web {
public static void main(String[] args) {
SpringApplication app = new SpringApplication(Web.class);
app.setWebApplicationType(WebApplicationType.REACTIVE);
SpringApplication.run(Web.class, args);
}
}
The whole class looks like this
After setting the application type, if I don't then call the SpringApplication in a static way, I get this:

Integration of JAX-WS to Spring Boot

I have an existing API which has both JAX-RS and JAX-WS. I want to migrate it into a Spring Boot application. What I've done for JAX-RS part is registering that class:
#GET
#Path("/ping")
#Produces("text/plain")
String ping();
into a Jersey Config which extends ResourceConfig. Here is the example from JAX-WS of same class:
#WebMethod(operationName = "Ping", action = "ping-app")
String ping();
Since I've used reference implementations of JAX-RS and JAX-WS I hope that it should be easy to migrate it into Spring Boot. I've easily done JAX-RS integration. Is there any such simple way to integrate JAX-WS too?
Ideally you'd want to use a Spring Boot Starter to assist you. According to their documentation:
Starters are a set of convenient dependency descriptors that you can
include in your application. You get a one-stop-shop for all the
Spring and related technology that you need, without having to hunt
through sample code and copy paste loads of dependency descriptors.
For example, if you want to get started using Spring and JPA for
database access, just include the spring-boot-starter-data-jpa
dependency in your project, and you are good to go.
Using this list of official and community starters, it looks like you have the following options:
spring-boot-starter-jersey should be able to handle JAX-RS
spring-boot-web-services might be able to handle JAX-WS. From what I understand, you might need to do things the 'Spring Web Services' way which is different to JAX-WS. It's been a while since I worked with Spring Web Services, so I might be incorrect on this point.
The Apache CXF starter can support both JAX-RS and JAX-WS which seems to meet your requirement. They have this guide for their spring boot integration for you to look at.
I didn't use this personally but it seems that spring provides starter pack for JAX-WS
Take a look at this artifact. It should be enough just to add this dependency to your project:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jersey</artifactId>
</dependency>
I have solved the problem by using this dependency
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-spring-boot-starter-jaxws</artifactId>
<version>3.3.6</version>
</dependency>
and add this bean in the configuration bean
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.xml.ws.Endpoint;
import org.apache.cxf.Bus;
import org.apache.cxf.jaxws.EndpointImpl;
#Configuration
public class WebServiceConfig{
#Autowired
private Bus bus;
#Bean
public Endpoint endpoint() {
EndpointImpl endpoint = new EndpointImpl(bus, new SpPortImpl());
endpoint.publish("/NpcdbService");
return endpoint;
}
}
SpPortImpl is the class annotated with #WebService

Spring Cloud Consul /refresh endpoint missing

I'm using Spring Cloud Consul for Distributed Configuration with Consul and all goes fine. All configuration is currently and successfully read from Consul server on startup of the application. But I can't reload this configuration for my app when some data on Consul changed because there is not /refresh endpoint. But here says "Sending a HTTP POST to /refresh will cause the configuration to be reloaded." As I understand it should be like for Spring Cloud Config Client but it doesn't. What did I miss?
You need to include the spring boot actuator
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
Or add #RefreshScope on your bean for e.g.
#Component
#RefreshScope
public class MyConsulConfig {
#Value("${consul.base.url}")
private String baseUrl;
}

Multiple logger library situation in Spring

I have just met situation like that, I created default Spring framework from Spring tool suite, it will use slf4j and log4j for log everything into console.
After that, I add Spring security oauth dependency to maven, this dependency tree like :
spring security oauth --> spring boot --> spring boot starter --> logback(another logger).
The problem is my project had a logger, now spring boot add another logger, this make my logger work so strange (I used log4j.xml in classpath, level of logger is info but it print everything in debug level).
After I exclude logback from spring security oauth dependency from maven, log worked great, but I'm afraid of if I removed logback from spring boot starter, somewhere in this lib need logback, does it throw ClassNotFoundException?
Thanks in advance!
Update:
I copied pom file of spring security oauth2 from it's folder:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>1.0.0.RELEASE</version>
</dependency>
Spring Security OAuth does not rely on Spring Boot. You probably have a different dependency that brings that.
Anyway, you can safely exclude logback, yes. Check also the documentation for more details.

Categories

Resources