Swagger 2 Issue - Spring Boot - java

I was using a tutorial and everything was working fine until I started dealing with swagger 2 dependencies.
I wonder now if there is a way to fix this.
SwaggerConfig:
package com.animes.apirest.config;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.VendorExtension;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import static springfox.documentation.builders.PathSelectors.regex;
import java.util.ArrayList;
#Configuration
#EnableSwagger2
public class SwaggerConfig {
#Bean
public Docket atividadeApi() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("com.atividades.apirest"))
.paths(regex("/api.*"))
.build()
.apiInfo(metaInfo());
}
private ApiInfo metaInfo() {
ApiInfo apiInfo = new ApiInfo(
"Atividades API REST",
"API REST de cadastro de atividades.",
"1.0",
"Terms of Service",
new Contact("João VR", "www.una.br/",
" "),
"Apache License Version 2.0",
"https://www.apache.org/licesen.html", new ArrayList<VendorExtension>()
);
return apiInfo;
}
}
pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.animes</groupId>
<artifactId>apirest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>apirest</name>
<description>Anime project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>20.0</version>
</dependency>
<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>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Error:
19:07:08.137 [Thread-0] DEBUG org.springframework.boot.devtools.restart.classloader.RestartClassLoader - Created RestartClassLoader org.springframework.boot.devtools.restart.classloader.RestartClassLoader#43899316
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.6.0)
2021-11-21 19:07:08.606 INFO 9840 --- [ restartedMain] com.animes.apirest.ApirestApplication : Starting ApirestApplication using Java 16.0.2 on DESKTOP-TIGCP3C with PID 9840 (C:\Program Files (x86)\eclipse\Workspace\apirest\target\classes started by Pichau in C:\Program Files (x86)\eclipse\Workspace\apirest)
2021-11-21 19:07:08.607 INFO 9840 --- [ restartedMain] com.animes.apirest.ApirestApplication : No active profile set, falling back to default profiles: default
2021-11-21 19:07:08.679 INFO 9840 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
2021-11-21 19:07:08.680 INFO 9840 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
2021-11-21 19:07:09.807 INFO 9840 --- [ restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2021-11-21 19:07:09.895 INFO 9840 --- [ restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 71 ms. Found 1 JPA repository interfaces.
2021-11-21 19:07:10.790 INFO 9840 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2021-11-21 19:07:10.813 INFO 9840 --- [ restartedMain] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2021-11-21 19:07:10.813 INFO 9840 --- [ restartedMain] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.55]
2021-11-21 19:07:10.965 INFO 9840 --- [ restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2021-11-21 19:07:10.966 INFO 9840 --- [ restartedMain] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 2286 ms
2021-11-21 19:07:11.194 INFO 9840 --- [ restartedMain] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [name: default]
2021-11-21 19:07:11.245 INFO 9840 --- [ restartedMain] org.hibernate.Version : HHH000412: Hibernate ORM core version 5.6.1.Final
2021-11-21 19:07:11.427 INFO 9840 --- [ restartedMain] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.1.2.Final}
2021-11-21 19:07:11.536 INFO 9840 --- [ restartedMain] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2021-11-21 19:07:11.746 INFO 9840 --- [ restartedMain] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
2021-11-21 19:07:11.776 INFO 9840 --- [ restartedMain] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.PostgreSQL10Dialect
2021-11-21 19:07:12.466 INFO 9840 --- [ restartedMain] o.h.e.t.j.p.i.JtaPlatformInitiator : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2021-11-21 19:07:12.475 INFO 9840 --- [ restartedMain] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2021-11-21 19:07:13.074 WARN 9840 --- [ restartedMain] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2021-11-21 19:07:13.266 INFO 9840 --- [ restartedMain] pertySourcedRequestMappingHandlerMapping : Mapped URL path [/v2/api-docs] onto method [springfox.documentation.swagger2.web.Swagger2Controller#getDocumentation(String, HttpServletRequest)]
2021-11-21 19:07:13.497 INFO 9840 --- [ restartedMain] o.s.b.d.a.OptionalLiveReloadServer : LiveReload server is running on port 35729
2021-11-21 19:07:13.717 INFO 9840 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2021-11-21 19:07:13.718 INFO 9840 --- [ restartedMain] d.s.w.p.DocumentationPluginsBootstrapper : Context refreshed
2021-11-21 19:07:13.740 INFO 9840 --- [ restartedMain] d.s.w.p.DocumentationPluginsBootstrapper : Found 1 custom documentation plugin(s)
2021-11-21 19:07:13.744 WARN 9840 --- [ restartedMain] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.context.ApplicationContextException: Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException: Cannot invoke "org.springframework.web.servlet.mvc.condition.PatternsRequestCondition.toString()" because the return value of "springfox.documentation.spi.service.contexts.Orderings.patternsCondition(springfox.documentation.RequestHandler)" is null
2021-11-21 19:07:13.747 INFO 9840 --- [ restartedMain] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
2021-11-21 19:07:13.751 INFO 9840 --- [ restartedMain] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown initiated...
2021-11-21 19:07:13.760 INFO 9840 --- [ restartedMain] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown completed.
2021-11-21 19:07:13.782 INFO 9840 --- [ restartedMain] o.apache.catalina.core.StandardService : Stopping service [Tomcat]
2021-11-21 19:07:13.796 INFO 9840 --- [ restartedMain] ConditionEvaluationReportLoggingListener :
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2021-11-21 19:07:13.826 ERROR 9840 --- [ restartedMain] o.s.boot.SpringApplication : Application run failed
org.springframework.context.ApplicationContextException: Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException: Cannot invoke "org.springframework.web.servlet.mvc.condition.PatternsRequestCondition.toString()" because the return value of "springfox.documentation.spi.service.contexts.Orderings.patternsCondition(springfox.documentation.RequestHandler)" is null
at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:181) ~[spring-context-5.3.13.jar:5.3.13]
at org.springframework.context.support.DefaultLifecycleProcessor.access$200(DefaultLifecycleProcessor.java:54) ~[spring-context-5.3.13.jar:5.3.13]
at org.springframework.context.support.DefaultLifecycleProcessor$LifecycleGroup.start(DefaultLifecycleProcessor.java:356) ~[spring-context-5.3.13.jar:5.3.13]
at java.base/java.lang.Iterable.forEach(Iterable.java:75) ~[na:na]
at org.springframework.context.support.DefaultLifecycleProcessor.startBeans(DefaultLifecycleProcessor.java:155) ~[spring-context-5.3.13.jar:5.3.13]
at org.springframework.context.support.DefaultLifecycleProcessor.onRefresh(DefaultLifecycleProcessor.java:123) ~[spring-context-5.3.13.jar:5.3.13]
at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:935) ~[spring-context-5.3.13.jar:5.3.13]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:586) ~[spring-context-5.3.13.jar:5.3.13]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:145) ~[spring-boot-2.6.0.jar:2.6.0]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:730) ~[spring-boot-2.6.0.jar:2.6.0]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:412) ~[spring-boot-2.6.0.jar:2.6.0]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:302) ~[spring-boot-2.6.0.jar:2.6.0]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1301) ~[spring-boot-2.6.0.jar:2.6.0]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1290) ~[spring-boot-2.6.0.jar:2.6.0]
at com.animes.apirest.ApirestApplication.main(ApirestApplication.java:10) ~[classes/:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:78) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:567) ~[na:na]
at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) ~[spring-boot-devtools-2.6.0.jar:2.6.0]
Caused by: java.lang.NullPointerException: Cannot invoke "org.springframework.web.servlet.mvc.condition.PatternsRequestCondition.toString()" because the return value of "springfox.documentation.spi.service.contexts.Orderings.patternsCondition(springfox.documentation.RequestHandler)" is null
at springfox.documentation.spi.service.contexts.Orderings$8.compare(Orderings.java:112) ~[springfox-spi-2.9.2.jar:null]
at springfox.documentation.spi.service.contexts.Orderings$8.compare(Orderings.java:109) ~[springfox-spi-2.9.2.jar:null]
at com.google.common.collect.ComparatorOrdering.compare(ComparatorOrdering.java:37) ~[guava-20.0.jar:na]
at java.base/java.util.TimSort.countRunAndMakeAscending(TimSort.java:355) ~[na:na]
at java.base/java.util.TimSort.sort(TimSort.java:220) ~[na:na]
at java.base/java.util.Arrays.sort(Arrays.java:1232) ~[na:na]
at com.google.common.collect.Ordering.sortedCopy(Ordering.java:855) ~[guava-20.0.jar:na]
at springfox.documentation.spring.web.plugins.WebMvcRequestHandlerProvider.requestHandlers(WebMvcRequestHandlerProvider.java:57) ~[springfox-spring-web-2.9.2.jar:null]
at springfox.documentation.spring.web.plugins.DocumentationPluginsBootstrapper$2.apply(DocumentationPluginsBootstrapper.java:138) ~[springfox-spring-web-2.9.2.jar:null]
at springfox.documentation.spring.web.plugins.DocumentationPluginsBootstrapper$2.apply(DocumentationPluginsBootstrapper.java:135) ~[springfox-spring-web-2.9.2.jar:null]
at com.google.common.collect.Iterators$7.transform(Iterators.java:750) ~[guava-20.0.jar:na]
at com.google.common.collect.TransformedIterator.next(TransformedIterator.java:47) ~[guava-20.0.jar:na]
at com.google.common.collect.TransformedIterator.next(TransformedIterator.java:47) ~[guava-20.0.jar:na]
at com.google.common.collect.MultitransformedIterator.hasNext(MultitransformedIterator.java:52) ~[guava-20.0.jar:na]
at com.google.common.collect.MultitransformedIterator.hasNext(MultitransformedIterator.java:50) ~[guava-20.0.jar:na]
at com.google.common.collect.ImmutableList.copyOf(ImmutableList.java:249) ~[guava-20.0.jar:na]
at com.google.common.collect.ImmutableList.copyOf(ImmutableList.java:209) ~[guava-20.0.jar:na]
at com.google.common.collect.FluentIterable.toList(FluentIterable.java:614) ~[guava-20.0.jar:na]
at springfox.documentation.spring.web.plugins.DocumentationPluginsBootstrapper.defaultContextBuilder(DocumentationPluginsBootstrapper.java:111) ~[springfox-spring-web-2.9.2.jar:null]
at springfox.documentation.spring.web.plugins.DocumentationPluginsBootstrapper.buildContext(DocumentationPluginsBootstrapper.java:96) ~[springfox-spring-web-2.9.2.jar:null]
at springfox.documentation.spring.web.plugins.DocumentationPluginsBootstrapper.start(DocumentationPluginsBootstrapper.java:167) ~[springfox-spring-web-2.9.2.jar:null]
at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:178) ~[spring-context-5.3.13.jar:5.3.13]
... 19 common frames omitted
Searching about I tried to change versions to 2.8.0, 2.7.0, 3.0.0... also returns error.
The application is an apirest with task list activities.

try upgrade version of springfox, add spring fox starter and remove #EnableSwagger2
Dependencies to be added
<!--springfox dependencies for api documentations in swagger -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
Remove anotations #EnableSwagger2
#Configuration
// #EnableSwagger2 // remove this annotation
public class SwaggerConfig { ... }
try on this link if there is no override changing in default path
http://localhost:{port}/swagger-ui/index.html

I have solved this issue in the Latest Spring Boot 2.6.7 version by following steps. Anyone can check who is facing this issue in 2.6.7 version.
Comment/Remove actuator dependency in pom.xml file
<!-- <dependency>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-starter-actuator</artifactId>-->
<!-- </dependency>-->
Add swagger dependency in pom.xml file
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
Swagger Configuration class change to
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
#Configuration
#EnableWebMvc
public class SwaggerConfig implements WebMvcConfigurer {
#Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2).select()
.apis(RequestHandlerSelectors.basePackage("com.companyname.app"))
.paths(PathSelectors.regex("/.*"))
.build().apiInfo(apiInfoMetaData());
}
private ApiInfo apiInfoMetaData() {
return new ApiInfoBuilder().title("NAME OF SERVICE")
.description("API Endpoint Decoration")
.contact(new Contact("Dev-Team", "https://www.dev-team.com/", "dev-team#gmail.com"))
.license("Apache 2.0")
.licenseUrl("http://www.apache.org/licenses/LICENSE-2.0.html")
.version("1.0.0")
.build();
}
}
Swagger apis URL
My case
http://localhost:8080/swagger-ui/index.html
Your case if you different port and project context path
http://localhost:{port}/your-context-path/swagger-ui/index.html

The springfox plugin is not compatible with PathPattern-based matching in Spring MVC which has replaced the previous Ant-based matching in the Spring Boot default settings. See e.g. here
https://stackoverflow.com/a/70037507/15496965
https://stackoverflow.com/a/69814964/15496965
You can try to set spring.mvc.pathmatch.matching-strategy=ant_path_matcher to flip that property back to its previous default value. But this won't help if you use actuators which are not effected by that property.
You can try to force the actuators back to Ant-based matching as explained in the second post. But I'd really not recommend that. Instead, you can use Spring Boot <= 2.5 for the moment or migrate to springdoc.

I know this does not solve your problem directly, but consider moving to springdoc. Springfox is so buggy at this point that is a pain to use. I've moved to springdoc 2 years ago because of its Spring WebFlux support and I am very happy about it. Additionally, it also supports Kotlin Coroutines, which I am not sure Springfox does.
If you decide to migrate, springdoc even has a migration guide.
Additionally, you are already using Spring Boot 2.6, which is very very recent (its release notes are from a couple of hours ago) and thus springdoc and Springfox might not support it already. So I would also suggest you use Spring Boot 2.5.7 instead.

Just add this configuration to your application.properties
spring.mvc.pathmatch.matching-strategy = ANT_PATH_MATCHER
then Run your application.
You need to edit one part, the base package name. As shown in the screenshot my base package name.
my package name
#Configuration
#EnableSwagger2
public class SwaggerConfig2 {
#Bean
public Docket docket() {
return new Docket(DocumentationType.SWAGGER_2)
.enable(true)
.apiInfo(new ApiInfoBuilder()
.title("Swagger Super")
.description("Swagger Description details")
.version("1.0").build())
.select()
.apis(RequestHandlerSelectors.basePackage("com.example.swagger.controller"))
.paths(PathSelectors.any()).build();
}
}
OR
#Configuration
#EnableSwagger2
public class SwaggerConfig2 {
#Bean
public Docket docket() {
return new Docket(DocumentationType.SWAGGER_2)
.enable(true)
.apiInfo(new ApiInfoBuilder()
.title("Swagger Super")
.description("Swagger Description details")
.version("1.0").build())
.select()
.apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
.paths(PathSelectors.any()).build();
}
}
And the Swagger Dependencies
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.1</version>
<scope>compile</scope>
</dependency>
Try like this, I hope it will work
Note: SNAPSHOT, M1, M2, M3, and M4 releases typically WORK IN PROGRESS. The Spring team is still working on them, Recommend NOT using them.

It should be added, when i add it resolved
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
show the below all swagger dependency
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-schema</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>

I had the same error, but I get down the org.springframework.boot to 2.5.8, and I had it on 2.7.1.
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.8</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
And the Swagger Dependencies
<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>
And It works!!!

Add #Enablewebmvc in #Configuration class
and remove all other dependencies and include only below one for swagger
Add #Enablewebmvc in #Configuration class
and remove all other dependencies and include only below one for swagger
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency> "

Just adding below to your application.properties works like magic
spring.mvc.pathmatch.matching-strategy = ANT_PATH_MATCHER

Related

Apache Spark with Java Spring boot : Exception in thread "restartedMain" java.lang.reflect.InvocationTargetException

I'm trying to retrieve data from an Oracle Database table, and I'm new to Apache Spark.
I must retrieve data from two databases with two tables (production DB and lower environment)
must compare the data in both tables column by column and then insert it into another audit table in a lower environment. Later, a job will update the lower environment with the updated data.
Due to the enormous amount of data, many tables with various schemas will contain trillions of records.
The problem is that I cannot connect to and retrieve records from the database.
Any possible solution would be greatly appreciated. Thanks
public static void main(String[] args) {
SpringApplication.run(ApacheSparkApplication.class, args);
SparkSession spark = SparkSession.builder().master("local[*]").appName("SparkandOracledbTest").getOrCreate();
Dataset<Row> oracleDF = spark.read().format("oracle")
.option("adbId", "jdbc:oracle:thin:#host:port/dbname")
.option("dbtable", "owner.tablename")
.option("user", "user")
.option("password", "password")
.load();
oracleDF.show();
2022-10-26 08:25:22.557 INFO 5448 --- [ restartedMain] o.s.jetty.server.handler.ContextHandler : Started o.s.j.s.ServletContextHandler#244c93d7{/metrics/json,null,AVAILABLE,#Spark}
2022-10-26 08:25:22.765 WARN 5448 --- [ restartedMain] o.apache.spark.sql.internal.SharedState : URL.setURLStreamHandlerFactory failed to set FsUrlStreamHandlerFactory
2022-10-26 08:25:22.766 INFO 5448 --- [ restartedMain] o.apache.spark.sql.internal.SharedState : Setting hive.metastore.warehouse.dir ('null') to the value of spark.sql.warehouse.dir.
2022-10-26 08:25:22.776 INFO 5448 --- [ restartedMain] o.apache.spark.sql.internal.SharedState : Warehouse path is 'file:/C:/Vijay/csv/spark-warehouse'.
2022-10-26 08:25:22.793 INFO 5448 --- [ restartedMain] o.s.jetty.server.handler.ContextHandler : Started o.s.j.s.ServletContextHandler#7b364643{/SQL,null,AVAILABLE,#Spark}
2022-10-26 08:25:22.794 INFO 5448 --- [ restartedMain] o.s.jetty.server.handler.ContextHandler : Started o.s.j.s.ServletContextHandler#76e99147{/SQL/json,null,AVAILABLE,#Spark}
2022-10-26 08:25:22.795 INFO 5448 --- [ restartedMain] o.s.jetty.server.handler.ContextHandler : Started o.s.j.s.ServletContextHandler#40a15ec9{/SQL/execution,null,AVAILABLE,#Spark}
2022-10-26 08:25:22.796 INFO 5448 --- [ restartedMain] o.s.jetty.server.handler.ContextHandler : Started o.s.j.s.ServletContextHandler#d385186{/SQL/execution/json,null,AVAILABLE,#Spark}
2022-10-26 08:25:22.798 INFO 5448 --- [ restartedMain] o.s.jetty.server.handler.ContextHandler : Started o.s.j.s.ServletContextHandler#3fa6b59a{/static/sql,null,AVAILABLE,#Spark}
Exception in thread "restartedMain" java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)
Caused by: java.lang.NoClassDefFoundError: org/codehaus/janino/InternalCompilerException
at org.apache.spark.sql.execution.datasources.DataSource$.lookupDataSource(DataSource.scala:675)
at org.apache.spark.sql.execution.datasources.DataSource$.lookupDataSourceV2(DataSource.scala:725)
at org.apache.spark.sql.DataFrameReader.load(DataFrameReader.scala:207)
at org.apache.spark.sql.DataFrameReader.load(DataFrameReader.scala:171)
at com.spark.csv.ApacheSparkApplication.main(ApacheSparkApplication.java:34)
... 5 more
Caused by: java.lang.ClassNotFoundException: org.codehaus.janino.InternalCompilerException
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
... 10 more
POM.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artif`actId>spring-boot-starter-web</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.spark/spark-core -->
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-core_2.13</artifactId>
<version>3.3.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-devtools -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.spark/spark-sql -->
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-sql_2.13</artifactId>
<version>3.3.0</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.codehaus.janino/janino -->
<dependency>
<groupId>org.codehaus.janino</groupId>
<artifactId>janino</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.codehaus.janino/commons-compiler -->
<dependency>
<groupId>org.codehaus.janino</groupId>
<artifactId>commons-compiler</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.oracle.ojdbc/ojdbc8 -->
<dependency>
<groupId>com.oracle.ojdbc</groupId>
<artifactId>ojdbc8</artifactId>
<version>19.3.0.0</version>
</dependency>
</dependencies>

How to create RESTful Web Service with Spring Boot and Apache Camel?

I'm learning Apache Camel in Spring Boot project and I try to create a Retful Webservice and the service is starting but the problem is that I get 404 when I call the endpoint.
#Component
#RequiredArgsConstructor
public class RestJavaDsl extends RouteBuilder {
private final WeatherDataProvider weatherDataProvider;
#Override
public void configure() throws Exception {
from("rest:get:javadsl/weather/{city}?produces=application/json")
.outputType(WeatherDto.class)
.process(this::getWeatherData);
}
private void getWeatherData(Exchange exchange) {
String city = exchange.getMessage().getHeader("city", String.class);
WeatherDto currentWeather = weatherDataProvider.getCurrentWeather(city);
Message message = new DefaultMessage(exchange.getContext());
message.setBody(currentWeather);
exchange.setMessage(message);
}
}
I created this class to hardcode some data:
#Component
public class WeatherDataProvider {
private static Map<String, WeatherDto> weatherData = new HashMap<>();
public WeatherDataProvider() {
WeatherDto dto = WeatherDto.builder().city("London").temp("10").unit("C").receivedTime(new Date().toString()).id(1).build();
weatherData.put("LONDON", dto);
}
public WeatherDto getCurrentWeather(String city) {
return weatherData.get(city.toUpperCase());
}
public void setCurrentWeather(WeatherDto dto) {
dto.setReceivedTime(new Date().toString());
weatherData.put(dto.getCity().toUpperCase(), dto);
}
}
#Data
#NoArgsConstructor
#AllArgsConstructor
#Builder
public class WeatherDto implements Serializable {
static int counter = 1;
private int id = counter++;
private String city;
private String temp;
private String unit;
private String receivedTime;
}
application.yml
camel:
component:
servlet:
mapping:
context-path: /services/*
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.dgs</groupId>
<artifactId>camel-rest-springboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>camel-rest-springboot</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
<camel.version>3.14.0</camel.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-spring-boot-starter</artifactId>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-test</artifactId>
<scope>test</scope>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-jackson</artifactId>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-jaxb</artifactId>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-servlet</artifactId>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-servlet-starter</artifactId>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-rest-starter</artifactId>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
The serviceis starting and this is the console log:
2022-01-24 20:01:40.353 INFO 15796 --- [ main] c.d.c.CamelRestSpringbootApplication : Starting CamelRestSpringbootApplication using Java 11.0.5 on pc-PC with PID 15796 (D:\Spring Boot\camel-rest-springboot\target\classes started by pc in D:\Spring Boot\camel-rest-springboot)
2022-01-24 20:01:40.357 INFO 15796 --- [ main] c.d.c.CamelRestSpringbootApplication : No active profile set, falling back to default profiles: default
2022-01-24 20:01:43.583 INFO 15796 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2022-01-24 20:01:43.604 INFO 15796 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2022-01-24 20:01:43.604 INFO 15796 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.56]
2022-01-24 20:01:43.820 INFO 15796 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2022-01-24 20:01:43.821 INFO 15796 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 3235 ms
2022-01-24 20:01:45.228 INFO 15796 --- [ main] o.a.c.c.s.CamelHttpTransportServlet : Initialized CamelHttpTransportServlet[name=CamelServlet, contextPath=]
2022-01-24 20:01:45.233 INFO 15796 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2022-01-24 20:01:45.592 INFO 15796 --- [ main] o.a.c.impl.engine.AbstractCamelContext : Message DataType is enabled on CamelContext: camel-1
2022-01-24 20:01:45.607 INFO 15796 --- [ main] o.a.c.impl.engine.AbstractCamelContext : Routes startup (total:1 started:1)
2022-01-24 20:01:45.607 INFO 15796 --- [ main] o.a.c.impl.engine.AbstractCamelContext : Started route1 (rest://get:javadsl/weather/%7Bcity%7D)
2022-01-24 20:01:45.607 INFO 15796 --- [ main] o.a.c.impl.engine.AbstractCamelContext : Apache Camel 3.14.0 (camel-1) started in 370ms (build:83ms init:269ms start:18ms)
2022-01-24 20:01:45.617 INFO 15796 --- [ main] c.d.c.CamelRestSpringbootApplication : Started CamelRestSpringbootApplication in 6.569 seconds (JVM running for 8.087)
But when I try to call the endpoint http://localhost:8080/services/javadsl/weather/london
It looks like this endpoint doesn't exist, the rest isn't created. I used debugger and the method getWeatherData() isn't called.
And I think this log is not ok: Started route1 (rest://get:javadsl/weather/%7Bcity%7D), it should be something like that: route1 started and consuming from servlet:/javadsl/weather/%7Bcity%7D
And the tutorial is from here: https://www.youtube.com/watch?v=spDjbC8mZf0&t=433s
Thank you in advance!
You can start by creating example project using official camel-archetype-spring-boot maven archetype. You can generate project based on the archetype using your IDE or from command-line using following maven command.
mvn archetype:generate -DarchetypeArtifactId="camel-archetype-spring-boot" -DarchetypeGroupId="org.apache.camel.archetypes" -DarchetypeVersion="3.14.0"
Now in the maven project file pom.xml add following block to the dependencies
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-jetty-starter</artifactId>
</dependency>
Note that version doesn't need to be specified as it is provided by camel-spring-boot-dependencies BOM (bill of materials) in dependencyManagement section.
After that you can create new file ConfigureCamelContext.java where we can configure our CamelContext and provide it with RestConfiguration. We can do this by implementing CamelContextConfiguration and #Component annotation.
The annotation tells spring-framework that it should create instance of the class and inject it to objects that request it based on its class-type or interface. Camel is configured to automatically ask spring-framework for these RestConfiguration instances which it will proceed to use configure CamelContext.
package com.example;
import org.apache.camel.CamelContext;
import org.apache.camel.spi.RestConfiguration;
import org.apache.camel.spring.boot.CamelContextConfiguration;
import org.springframework.stereotype.Component;
#Component
public class ConfigureCamelContext implements CamelContextConfiguration {
#Override
public void beforeApplicationStart(CamelContext camelContext) {
RestConfiguration restConfiguration = new RestConfiguration();
restConfiguration.setApiComponent("jetty");
restConfiguration.setApiHost("localhost");
restConfiguration.setPort(8081);
camelContext.setRestConfiguration(restConfiguration);
}
#Override
public void afterApplicationStart(CamelContext camelContext) {
}
}
This should work with RestDSL and rest-component.
Edit MySpringBootRouter.java to something like this:
package com.example;
import org.apache.camel.Exchange;
import org.apache.camel.builder.RouteBuilder;
import org.springframework.stereotype.Component;
#Component
public class MySpringBootRouter extends RouteBuilder {
static final String CONTET_TYPE_TEXT = "text/plain";
#Override
public void configure() {
rest()
.get("/hello/")
.route()
.setHeader(Exchange.CONTENT_TYPE, constant(CONTET_TYPE_TEXT))
.setBody().constant("Hello world")
.end()
.endRest()
.get("/hello/{name}")
.route()
.setHeader(Exchange.CONTENT_TYPE, constant(CONTET_TYPE_TEXT))
.setBody().simple("Hello ${headers.name}")
.end()
.endRest();
from("rest:get:test?produces=plain/text")
.setBody().constant("Hello from JavaDSL");
}
}
Comment out the class under src/test/java/<grouId>/ as the example test provided by the archetype is not applicable for for MySpringBootRouter and will interrupt build.
To run the project you can use command
mvn spring-boot:run
Now you should be able to open up localhost:8081/hello, localhost:8081/hello/<Name> or localhost:8081/test get response in plain text.

Why can't Postman find my Spring REST API?

I created a very basic Spring Boot project with REST APIs. I tried connecting it to my Angular app but it was getting some CORS security error so I switched to Postman. I'm trying to test it using Postman but I keep receiving a 404 not found error on Postman. Why am I not able to connect to my backend and post to tester function?
controller
package controllers;
#RestController
public class Handler {
#PostMapping("/tester/{id}")
public String tester(#PathVariable(value="id")long userid ){
System.out.println("in tester");
System.out.println(userid);
return "succeeded test";
}
}
main
package halloween.contest;
#SpringBootApplication(exclude = {DataSourceAutoConfiguration.class,
SecurityAutoConfiguration.class})
public class ContestApplication {
public static void main(String[] args) {
SpringApplication.run(ContestApplication.class, args);
}
}
application.properties
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration
spring.data.rest.base-path=/
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.6</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>halloween</groupId>
<artifactId>contest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>contest</name>
<description>halloween picture contest</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
console
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.5.6)
2021-10-26 15:44:12.823 INFO 18760 --- [ main] halloween.contest.ContestApplication : Starting ContestApplication using Java 1.8.0_144 on DESKTOP-VB80TS0 with PID 18760 (C:\Users\Sergio Rodríguez\Documents\Halloween\contest\target\classes started by Sergio Rodríguez in C:\Users\Sergio Rodríguez\Documents\Halloween\contest)
2021-10-26 15:44:12.827 INFO 18760 --- [ main] halloween.contest.ContestApplication : No active profile set, falling back to default profiles: default
2021-10-26 15:44:16.013 INFO 18760 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2021-10-26 15:44:16.043 INFO 18760 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2021-10-26 15:44:16.043 INFO 18760 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.54]
2021-10-26 15:44:16.310 INFO 18760 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2021-10-26 15:44:16.310 INFO 18760 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 3372 ms
2021-10-26 15:44:19.326 INFO 18760 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2021-10-26 15:44:19.342 INFO 18760 --- [ main] halloween.contest.ContestApplication : Started ContestApplication in 7.201 seconds (JVM running for 7.946)
2021-10-26 15:45:45.138 INFO 18760 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2021-10-26 15:45:45.139 INFO 18760 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2021-10-26 15:45:45.157 INFO 18760 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 17 ms
equivalent cURL command
curl --location --request POST 'localhost:8080/tester/69'
Postman
Answering my own question now that I figured it out. It wasn't apparent based on the code I provided but I'll leave my post for future help (wording helps).
the problem was visibility of packages. I had my controller class in a different package from the main class.
To fix the CORS issue, next to #PostMapping("/tester/{id}") add #CrossOrigin(origins = "http://localhost:<port>") where port is the port from which your Angular app is running.
In your target URL, make sure you've included the default Spring Boot port of 8080, e.g. http://localhost:8080/tester/123 and obviously, please double check that you're doing a POST and not a GET.
got a similar problem..then added the below annotations on top of springbootappication where main() exists, then it's working fine for me.
#EnableWebMvc
#ComponentScan("com.*.*")
Also in postman make the below changes...
Disable/uncheck File --> Proxy --> "Use the system proxy"
Disable/uncheck File --> General --> "SSL Certificate Verification"

Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException:

I m trying to run my project Spring boot with mysql and flyway
My environement:
Linux 18.04
Java 1.8.0_201
Spring boot and this my pom.xml
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.3.1.RELEASE
com.mhanzouli
tutorial
0.0.1-SNAPSHOT
tutorial
Test Swagger/Actuator/Log4j2/Flyway
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>jakarta.validation</groupId>
<artifactId>jakarta.validation-api</artifactId>
<version>2.0.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
application.yml
spring:
datasource:
url: jdbc:mysql://localhost:3306/tutorial_bd?useSSL=false&serverTimezone=UTC&useLegacyDatetimeCode=false
username: root
password:
jpa:
show-sql: true
hibernate:
ddl-auto: validate
naming-strategy: org.hibernate.cfg.ImprovedNamingStrategy
properties:
hibernate:
dialect: org.hibernate.dialect.MySQL5InnoDBDialect
flyway:
url: jdbc:mysql://localhost:3306/tutorial_bd
user: "root"
password:
baselineOnMigrate: true
check-location: true
locations: classpath:db/migration
enabled: true
V1__init.sql
CREATE TABLE users (
id bigint(20) NOT NULL AUTO_INCREMENT,
username varchar(100) NOT NULL,
first_name varchar(50) NOT NULL,
last_name varchar(50) DEFAULT NULL,
PRIMARY KEY (id),
UNIQUE KEY UK_username (username)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
User.java
package com.mhanzouli.tutorial.domain;
import javax.persistence.*;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;
#Entity
#Table(name = "users")
public class User {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
#NotBlank
#Column(unique = true)
#Size(min = 1, max = 100)
private String username;
#NotBlank
#Size(max = 50)
private String firstName;
#Size(max = 50)
private String lastName;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
this is my project structure
And when i execute
mvn spring-boot:run
i got this error
2020-06-20 21:19:54.540 INFO 13594 --- [ main] c.m.tutorial.TutorialApplication : Starting TutorialApplication on marwen with PID 13594 (/home/marwen/Bureau/projects/tutorial/target/classes started by marwen in /home/marwen/Bureau/projects/tutorial)
2020-06-20 21:19:54.543 INFO 13594 --- [ main] c.m.tutorial.TutorialApplication : No active profile set, falling back to default profiles: default
2020-06-20 21:19:55.337 INFO 13594 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFERRED mode.
2020-06-20 21:19:55.364 INFO 13594 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 19ms. Found 0 JPA repository interfaces.
2020-06-20 21:19:55.962 INFO 13594 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2020-06-20 21:19:55.971 INFO 13594 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2020-06-20 21:19:55.971 INFO 13594 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.36]
2020-06-20 21:19:56.045 INFO 13594 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2020-06-20 21:19:56.046 INFO 13594 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1452 ms
2020-06-20 21:19:56.196 INFO 13594 --- [ main] o.f.c.internal.license.VersionPrinter : Flyway Community Edition 6.4.4 by Redgate
2020-06-20 21:19:56.310 INFO 13594 --- [ main] o.f.c.internal.database.DatabaseFactory : Database: jdbc:mysql://localhost:3306/tutorial_bd (MySQL 5.5)
2020-06-20 21:19:56.366 WARN 13594 --- [ main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'flywayInitializer' defined in class path resource [org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration$FlywayConfiguration.class]: Invocation of init method failed; nested exception is org.flywaydb.core.internal.license.FlywayEditionUpgradeRequiredException: Flyway Pro Edition or MariaDB upgrade required: MariaDB 10.1 is no longer supported by Flyway Community Edition, but still supported by Flyway Pro Edition.
2020-06-20 21:19:56.369 INFO 13594 --- [ main] o.apache.catalina.core.StandardService : Stopping service [Tomcat]
2020-06-20 21:19:56.382 INFO 13594 --- [ main] ConditionEvaluationReportLoggingListener :
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-06-20 21:19:56.392 ERROR 13594 --- [ main] o.s.boot.SpringApplication : Application run failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'flywayInitializer' defined in class path resource [org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration$FlywayConfiguration.class]: Invocation of init method failed; nested exception is org.flywaydb.core.internal.license.FlywayEditionUpgradeRequiredException: Flyway Pro Edition or MariaDB upgrade required: MariaDB 10.1 is no longer supported by Flyway Community Edition, but still supported by Flyway Pro Edition.
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1796) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:595) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:226) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:310) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1109) ~[spring-context-5.2.7.RELEASE.jar:5.2.7.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:869) ~[spring-context-5.2.7.RELEASE.jar:5.2.7.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:551) ~[spring-context-5.2.7.RELEASE.jar:5.2.7.RELEASE]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:143) ~[spring-boot-2.3.1.RELEASE.jar:2.3.1.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:758) [spring-boot-2.3.1.RELEASE.jar:2.3.1.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:750) [spring-boot-2.3.1.RELEASE.jar:2.3.1.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.3.1.RELEASE.jar:2.3.1.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) [spring-boot-2.3.1.RELEASE.jar:2.3.1.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1237) [spring-boot-2.3.1.RELEASE.jar:2.3.1.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) [spring-boot-2.3.1.RELEASE.jar:2.3.1.RELEASE]
at com.mhanzouli.tutorial.TutorialApplication.main(TutorialApplication.java:10) [classes/:na]
Caused by: org.flywaydb.core.internal.license.FlywayEditionUpgradeRequiredException: Flyway Pro Edition or MariaDB upgrade required: MariaDB 10.1 is no longer supported by Flyway Community Edition, but still supported by Flyway Pro Edition.
at org.flywaydb.core.internal.database.base.Database.ensureDatabaseNotOlderThanOtherwiseRecommendUpgradeToFlywayEdition(Database.java:173) ~[flyway-core-6.4.4.jar:na]
at org.flywaydb.core.internal.database.mysql.MySQLDatabase.ensureSupported(MySQLDatabase.java:287) ~[flyway-core-6.4.4.jar:na]
at org.flywaydb.core.Flyway.execute(Flyway.java:514) ~[flyway-core-6.4.4.jar:na]
at org.flywaydb.core.Flyway.migrate(Flyway.java:159) ~[flyway-core-6.4.4.jar:na]
at org.springframework.boot.autoconfigure.flyway.FlywayMigrationInitializer.afterPropertiesSet(FlywayMigrationInitializer.java:65) ~[spring-boot-autoconfigure-2.3.1.RELEASE.jar:2.3.1.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1855) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1792) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE]
... 19 common frames omitted
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 10.089 s
[INFO] Finished at: 2020-06-20T21:19:56+01:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:2.3.1.RELEASE:run (default-cli) on project tutorial: Application finished with exit code: 1 -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
The exception says "MariaDB 10.1 is no longer supported". Use newer version of Maria DB, e.g. the current version 10.5.3, and corresponding version of JDBC driver for it.

Spring Boot + ReactiveCosmosRepository + java.lang.NoSuchMethodError: reactor.util.concurrent.Queues.empty()Ljava/util/function/Supplier

I am implementing Cosmos DB in my Spring Boot project, and am getting error while connecting to Azure Cosmos DB.
POM.xml
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-cosmosdb-spring-boot-starter</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>spring-data-cosmosdb</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-documentdb</artifactId>
<version>2.4.6</version>
</dependency>
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
Repository
package com.staples.ctg.repository.cosmos;
import org.springframework.stereotype.Repository;
import com.microsoft.azure.spring.data.cosmosdb.repository.ReactiveCosmosRepository;
import com.staples.ctg.service.dto.cache.GenericCacheDTO;
#Repository
public interface SpotCacheRepository extends ReactiveCosmosRepository <GenericCacheDTO, String> {
}
applicationProperties.yml
azure:
cosmosdb:
uri: Cosmos URI
key: Key
database: DB Name that I created
DTO
package com.staples.ctg.service.dto.cache;
#Document(collection="cosmos")
public class GenericCacheDTO {
#Id
private String id;
#PartitionKey
private String accountId;
}
When I spin up locally, I am getting below error
2020-02-14 12:20:32.922 INFO 22777 --- [ restartedMain] c.a.d.c.internal.RxDocumentClientImpl : Initializing DocumentClient with serviceEndpoint [<Azure Cosmos URI>], connectionPolicy [ConnectionPolicy{requestTimeoutInMillis=60000, mediaRequestTimeoutInMillis=300000, connectionMode=DIRECT, maxPoolSize=1000, idleConnectionTimeoutInMillis=60000, userAgentSuffix=';spring-data/2.2.0;8d0607104487ba8c93ae7a32c5e20645d1bb21edf269625ead71da72d4877d65;', retryOptions=RetryOptions{maxRetryAttemptsOnThrottledRequests=9, maxRetryWaitTimeInSeconds=30}, enableEndpointDiscovery=true, preferredLocations=null, usingMultipleWriteLocations=false, inetSocketProxyAddress=null}], consistencyLevel [null], directModeProtocol [Tcp]
2020-02-14 12:20:32.922 DEBUG 22777 --- [ restartedMain] reactor.util.Loggers$LoggerFactory : Using Slf4j logging framework
2020-02-14 12:20:32.979 DEBUG 22777 --- [ restartedMain] c.a.d.c.internal.GlobalEndpointManager : registering a refresh in [300000] ms
2020-02-14 12:20:33.034 DEBUG 22777 --- [ parallel-1] c.a.d.c.internal.GlobalEndpointManager : startRefreshLocationTimerAsync() - Invoking refresh, I was registered on [2020-02-14T12:20:32.985]
2020-02-14 12:20:33.035 INFO 22777 --- [ parallel-1] c.a.d.c.internal.RxDocumentClientImpl : Getting database account endpoint from https:<Azure Cosmos URI>
2020-02-14 12:20:33.123 DEBUG 22777 --- [ parallel-1] reactor.netty.tcp.TcpResources : [http] resources will use the default LoopResources: DefaultLoopResources {prefix=reactor-http, daemon=true, selectCount=8, workerCount=8}
2020-02-14 12:20:33.123 DEBUG 22777 --- [ parallel-1] reactor.netty.tcp.TcpResources : [http] resources will use the default ConnectionProvider: PooledConnectionProvider {name=http, poolFactory=reactor.netty.resources.ConnectionProvider$$Lambda$2068/965477132#65222f44}
2020-02-14 12:20:33.130 DEBUG 22777 --- [ parallel-1] r.netty.resources.DefaultLoopEpoll : Default Epoll support : false
2020-02-14 12:20:33.132 DEBUG 22777 --- [ parallel-1] r.netty.resources.DefaultLoopKQueue : Default KQueue support : false
2020-02-14 12:20:33.162 DEBUG 22777 --- [ parallel-1] r.n.resources.PooledConnectionProvider : Creating new client pool [reactor-netty-connection-pool] for <Azure Cosmos URI>
2020-02-14 12:20:33.183 ERROR 22777 --- [ parallel-1] reactor.core.scheduler.Schedulers : Scheduler worker in group main failed with an uncaught exception
java.lang.NoSuchMethodError: reactor.util.concurrent.Queues.empty()Ljava/util/function/Supplier;
at reactor.netty.internal.shaded.reactor.pool.SimpleFifoPool.<clinit>(SimpleFifoPool.java:35)
at reactor.netty.internal.shaded.reactor.pool.PoolBuilder.build(PoolBuilder.java:333)
at reactor.netty.internal.shaded.reactor.pool.PoolBuilder.fifo(PoolBuilder.java:321)
at reactor.netty.resources.ConnectionProvider.lambda$fixed$3(ConnectionProvider.java:197)
at reactor.netty.resources.PooledConnectionProvider$PooledConnectionAllocator.<init>(PooledConnectionProvider.java:237)
at reactor.netty.resources.PooledConnectionProvider.lambda$acquire$2(PooledConnectionProvider.java:154)
at java.util.concurrent.ConcurrentHashMap.computeIfAbsent(ConcurrentHashMap.java:1660)
at reactor.netty.resources.PooledConnectionProvider.lambda$acquire$3(PooledConnectionProvider.java:148)
at reactor.core.publisher.MonoCreate.subscribe(MonoCreate.java:54)
at reactor.netty.http.client.HttpClientConnect$MonoHttpConnect.lambda$subscribe$0(HttpClientConnect.java:319)
at reactor.core.publisher.MonoCreate.subscribe(MonoCreate.java:54)
at reactor.core.publisher.Mono.subscribe(Mono.java:3088)
at reactor.core.publisher.FluxRetryPredicate$RetryPredicateSubscriber.resubscribe(FluxRetryPredicate.java:123)
at reactor.core.publisher.MonoRetryPredicate.subscribe(MonoRetryPredicate.java:51)
at reactor.netty.http.client.HttpClientConnect$MonoHttpConnect.subscribe(HttpClientConnect.java:322)
at reactor.core.publisher.MonoFlatMapMany.subscribe(MonoFlatMapMany.java:49)
at reactor.core.publisher.MonoSingle.subscribe(MonoSingle.java:58)
at reactor.core.publisher.MonoFlatMap.subscribe(MonoFlatMap.java:60)
at reactor.core.publisher.MonoMapFuseable.subscribe(MonoMapFuseable.java:59)
at reactor.core.publisher.MonoOnErrorResume.subscribe(MonoOnErrorResume.java:44)
at reactor.core.publisher.FluxSourceMono.subscribe(FluxSourceMono.java:46)
at reactor.core.publisher.MonoSingle.subscribe(MonoSingle.java:58)
at reactor.core.publisher.MonoDefer.subscribe(MonoDefer.java:53)
at reactor.core.publisher.Mono.subscribe(Mono.java:3088)
at reactor.core.publisher.FluxRetryWhen.subscribe(FluxRetryWhen.java:82)
at reactor.core.publisher.MonoRetryWhen.subscribe(MonoRetryWhen.java:50)
at reactor.core.publisher.FluxSourceMono.subscribe(FluxSourceMono.java:46)
at reactor.core.publisher.FluxOnErrorResume.subscribe(FluxOnErrorResume.java:47)
at reactor.core.publisher.FluxMap.subscribe(FluxMap.java:62)
at reactor.core.publisher.FluxPeek.subscribe(FluxPeek.java:83)
at reactor.core.publisher.FluxMap.subscribe(FluxMap.java:62)
at reactor.core.publisher.FluxPeek.subscribe(FluxPeek.java:83)
at reactor.core.publisher.FluxDefer.subscribe(FluxDefer.java:55)
at reactor.core.publisher.FluxPeek.subscribe(FluxPeek.java:83)
at reactor.core.publisher.MonoSingle.subscribe(MonoSingle.java:58)
at reactor.core.publisher.MonoOnErrorResume.subscribe(MonoOnErrorResume.java:44)
at reactor.core.publisher.MonoFlatMap.subscribe(MonoFlatMap.java:60)
at reactor.core.publisher.MonoFlatMap$FlatMapMain.onNext(MonoFlatMap.java:150)
at reactor.core.publisher.MonoDelay$MonoDelayRunnable.run(MonoDelay.java:117)
at reactor.core.scheduler.SchedulerTask.call(SchedulerTask.java:50)
at reactor.core.scheduler.SchedulerTask.call(SchedulerTask.java:27)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
2020-02-14 12:20:46.801 WARN 22777 --- [scoveryClient-1] c.netflix.discovery.TimedSupervisorTask : task supervisor timed out
java.util.concurrent.TimeoutException: null
This looks like a reactor-core incorrect version issue.
This dependency might be bringing an older version of reactor-core.
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-cosmosdb-spring-boot-starter</artifactId>
<version>2.2.2</version>
</dependency>
Two ways to solve this issue:
Try excluding reactor-core from this dependency.
Try adding reactor-core dependency - 3.3.0 or higher.

Categories

Resources