JHipster registry : build error - jhipster-registry

I try to build and run jhipster registry, but I get an error :
mvn package -Pprod
I get following errors in test :
Tests in error:
SwaggerBasePathRewritingFilterTest.<init>:20 » Unresolved compilation problem...
SwaggerBasePathRewritingFilterTest.<init>:20 » Unresolved compilation problem...
SwaggerBasePathRewritingFilterTest.<init>:20 » Unresolved compilation problem...
SwaggerBasePathRewritingFilterTest.<init>:20 » Unresolved compilation problem...
AccountResourceTest.setup:33 » NoClassDefFound HttpServletRequest
AccountResourceTest.setup:33 » NoClassDefFound HttpServletRequest
AccountResourceTest.setup:33 » NoClassDefFound HttpServletRequest
AccountResourceTest.setup:33 » NoClassDefFound HttpServletRequest
UserJWTControllerTest.setup:39 » NoClassDefFound HttpServletResponse
UserJWTControllerTest.setup:39 » NoClassDefFound HttpServletResponse
UserJWTControllerTest.setup:39 » NoClassDefFound HttpServletResponse
ExceptionTranslatorTest.setup:37 » NoClassDefFound HttpServletRequest
ExceptionTranslatorTest.setup:37 » NoClassDefFound HttpServletRequest
ExceptionTranslatorTest.setup:37 » NoClassDefFound HttpServletRequest
ExceptionTranslatorTest.setup:37 » NoClassDefFound HttpServletRequest
ExceptionTranslatorTest.setup:37 » NoClassDefFound HttpServletRequest
If I skip tests, build is ok, but I got same error when I launch :
./mvnw -Pdev
2018-02-16 17:52:22.011 WARN 5354 --- [ restartedMain] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'undertowEmbeddedServletContainerFactory' defined in class path resource [org/springframework/boot/autoconfigure/web/EmbeddedServletContainerAutoConfiguration$EmbeddedUndertow.class]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'webConfigurer' defined in file [/home/denis/ngworkspace/jhipster-registry/target/classes/io/github/jhipster/registry/config/WebConfigurer.class]: Bean instantiation via constructor failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [io.github.jhipster.registry.config.WebConfigurer$$EnhancerBySpringCGLIB$$a8d0938a]: Constructor threw exception; nested exception is java.lang.Error: Unresolved compilation problems:
The import io.undertow.UndertowOptions cannot be resolved
ServletContext cannot be resolved to a type
ServletException cannot be resolved to a type
DispatcherType cannot be resolved to a type
DispatcherType cannot be resolved to a variable
DispatcherType cannot be resolved to a variable
DispatcherType cannot be resolved to a variable
The type io.undertow.Undertow$Builder cannot be resolved. It is indirectly referenced from required .class files
This lambda expression refers to the missing type Undertow$Builder
UndertowOptions cannot be resolved to a variable
ServletContext cannot be resolved to a type
DispatcherType cannot be resolved to a type
FilterRegistration cannot be resolved to a type
ServletContext cannot be resolved to a type
DispatcherType cannot be resolved to a type
FilterRegistration cannot be resolved to a type
ServletRegistration cannot be resolved to a type
2018-02-16 17:52:22.032 ERROR 5354 --- [ restartedMain] o.s.boot.SpringApplication : Application startup failed
org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'undertowEmbeddedServletContainerFactory' defined in class path resource [org/springframework/boot/autoconfigure/web/EmbeddedServletContainerAutoConfiguration$EmbeddedUndertow.class]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'webConfigurer' defined in file [/home/denis/ngworkspace/jhipster-registry/target/classes/io/github/jhipster/registry/config/WebConfigurer.class]: Bean instantiation via constructor failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [io.github.jhipster.registry.config.WebConfigurer$$EnhancerBySpringCGLIB$$a8d0938a]: Constructor threw exception; nested exception is java.lang.Error: Unresolved compilation problems:
The import io.undertow.UndertowOptions cannot be resolved
ServletContext cannot be resolved to a type
ServletException cannot be resolved to a type
...
Strangely, when I execute
mvn clean package -Pprod
build is ok, but it does not launch (same error).
I use registry in version 3.2.4, cloned from github, on Linux.
How build registry without error ? Do I forget something ?
Thanks, Denis

I found the solution. I added these dependencies in pom.xml :
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
<scope>provided</scope>
</dependency>
and all work. These dependencies were nevertheless obtained transitively, and I don't known why I have to add explicitly. Anybody has the response ?
Denis

If you were running with Eclipse:
in Eclipse select Project Properties->Maven->Active Maven Profiles (comma separated), and make it dev
That triggers a maven update of the project and everything compiles
No need to put dependencies in the pom

Do that is work
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
<scope>provided</scope>
</dependency>
#Atatorus

Related

SpringBoot: Cannot load driver class: org.postgresql.Driver

I have just created a new SpringBoot application (Java11) and am trying to connect to a Postgres database.
When I start the SpringBoot application, there is an error trying to connect to the database. The error reports that it Cannot load driver class: org.postgresql.Driver.
Question
How do I change my configuration below to get the SpringBoot application to connect to the database?
database version
PostgreSQL 12.6 on x86_64-apple-darwin16.7.0, compiled by Apple LLVM
version 8.1.0 (clang-802.0.42), 64-bit
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>
application.properties
# pims datasource
spring.datasource.url=jdbc:postgresql://localhost:5432/pims
spring.datasource.username=postgres
spring.datasource.password=
spring.datasource.driverClassName=org.postgresql.Driver
#spring.datasource.driver-class-name=org.postgresql.Driver
#spring.jpa.database-platform=postgres
spring.jpa.show-sql=true
spring.datasource.dbcp2.test-while-idle=true
spring.datasource.dbcp2.validation-query=select 1
#spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.naming.implicit-strategy=org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyHbmImpl
spring.jpa.hibernate.naming.physical-strategy=org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy
When I run the #SpringBootApplication class, SpringBoot starts to start up as expected, but gets the following error.
error
Error starting ApplicationContext. To display the conditions report
re-run your application with 'debug' enabled. 2021-06-15 11:58:51.770
ERROR 68967 --- [ main] o.s.boot.SpringApplication
: Application run failed
org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'dataSourceScriptDatabaseInitializer'
defined in class path resource
[org/springframework/boot/autoconfigure/sql/init/DataSourceInitializationConfiguration.class]:
Unsatisfied dependency expressed through method
'dataSourceScriptDatabaseInitializer' parameter 0; nested exception is
org.springframework.beans.factory.BeanCreationException: Error
creating bean with name 'dataSource' defined in class path resource
[org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]:
Bean instantiation via factory method failed; nested exception is
org.springframework.beans.BeanInstantiationException: Failed to
instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method
'dataSource' threw exception; nested exception is
java.lang.IllegalStateException: Cannot load driver class:
org.postgresql.Driver
If I remove the following entry (i.e. don't define the driver in application.properties and just have the pom dependency):
spring.datasource.driverClassName=org.postgresql.Driver
I then get the following error:
Failed to load driver class org.postgresql.Driver in either of HikariConfig class loader or Thread context classloader
When I expend the external dependency tree, I didn't find any jar for postgressql. After reimport maven dependency, it is added correctly and problem is gone. It is fine now.
You do not need any specific configuration to connect database unless your repository and entity packages are not pulled from another dependency.
I would suggest you to check your connection between your computer and maven repository. You should be missing your postgre dependecy somehow.
(You also would try providing specific version of postgresql artifact)

Exception while building a war file via (maven install) for a spring boot project with mongodb

I am trying to build a spring-boot application with mongodb. I excluded the spring-boot-starter-data-mongodb dependency and using mongodb-driver 3.12.5.
My application.properties file contains only spring.data.mongodb.uri=connection _string.
With this above configuration I am unable to build a war file to deploy in server. It is giving the following error.
java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mongo' defined in class path resource [org/springframework/boot/autoconfigure/mongo/MongoAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.mongodb.client.MongoClient]: Factory method 'mongo' threw exception; nested exception is java.lang.NoClassDefFoundError: com/mongodb/connection/DefaultClusterFactory
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.mongodb.client.MongoClient]: Factory method 'mongo' threw exception; nested exception is java.lang.NoClassDefFoundError: com/mongodb/connection/DefaultClusterFactory
Caused by: java.lang.NoClassDefFoundError: com/mongodb/connection/DefaultClusterFactory
Caused by: java.lang.ClassNotFoundException: com.mongodb.connection.DefaultClusterFactory
please help, thanks.
This is issue was solved by replacing the mongodb-driver dependency with mongodb-java-driver dependency
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.6.3</version>
</dependency>
as for the situation i faced i did not add a dependency that i am using in the application. that was the mistake.

springboot rest swagger :error in initialising bean

I am using swagger to document my spring boot rest application
Using
spring-boot-starter-web
spring-boot-starter-data-rest
springfox-swagger2 2.9.2
springfox-swagger 2.9.2
Code is building fine but when I run it it is throwing error below
org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'requestMappingHandlerMapping' defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration
$EnableWebMvcConfiguration.class]:
Unsatisfied dependency expressed through method 'requestMappingHandlerMapping' parameter 1;
nested exception is org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'mvcConversionService' defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration
$EnableWebMvcConfiguration.class]:
Bean instantiation via factory method failed;
nested exception is org.springframework.beans.BeanInstantiationException:
Failed to instantiate [org.springframework.format.support.FormattingConversionService]:
Factory method 'mvcConversionService' threw exception;
nested exception is org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'jsonSchemaConverter' defined in class path resource [org/springframework/data/rest/webmvc/config/RepositoryRestMvcConfiguration.class]:
Bean instantiation via factory method failed;
nested exception is org.springframework.beans.BeanInstantiationException:
Failed to instantiate [org.springframework.data.rest.webmvc.json.PersistentEntityToJsonSchemaConverter]:
Factory method 'jsonSchemaConverter' threw exception;
nested exception is org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'defaultConversionService' defined in class path resource [org/springframework/data/rest/webmvc/config/RepositoryRestMvcConfiguration.class]:
Bean instantiation via factory method failed;
nested exception is org.springframework.beans.BeanInstantiationException:
Failed to instantiate [org.springframework.format.support.DefaultFormattingConversionService]:
Factory method 'defaultConversionService' threw exception;
nested exception is org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'repositoryInvokerFactory' defined in class path resource [org/springframework/data/rest/webmvc/config/RepositoryRestMvcConfiguration.class]:
Bean instantiation via factory method failed;
nested exception is org.springframework.beans.BeanInstantiationException:
nested exception is org.springframework.beans.BeanInstantiationException:
Failed to instantiate [org.springframework.data.repository.support.RepositoryInvokerFactory]:
Factory method 'repositoryInvokerFactory' threw exception;
nested exception is java.lang.NoSuchMethodError: org.springframework.plugin.core.PluginRegistry.of(Ljava/util/List;)Lorg/springframework/plugin/core/PluginRegistry;
I had the same problem. Here is my fix:
TL/DR:
Springfox uses an outdated version of spring-plugin-core and is incompatible with spring boot 2.2.5. Springfox seems to be no longer maintained; migrate to a different spring-swagger-bridge, such as Springdoc-openapi: https://springdoc.github.io/springdoc-openapi-demos/
Relevant output
nested exception is org.springframework.beans.BeanInstantiationException:
Failed to instantiate [org.springframework.data.repository.support.RepositoryInvokerFactory]:
Factory method 'repositoryInvokerFactory' threw exception; nested exception is java.lang.NoSuchMethodError:
'org.springframework.plugin.core.PluginRegistry org.springframework.plugin.core.PluginRegistry.of(java.util.List)'
From NoSuchMethodError: org.springframework.plugin.core.PluginRegistry.getPluginOrDefaultFor:
...a wild guess would be that the last working version of springfox (working for you and us) was compiled with a Spring boot version inferior to 2.2.2. That boot version had a different method signature for getPluginOrDefaultFor (or possibly the method didn't exist at all).
Cause
springfox imports version 1.2.0 of the spring-plugin-core. Spring Boot 2.2.5 needs version 2.0.0.RELEASE
springfox wins the import lottery; test fails b/c Spring Boot can't find its method.
explicitly importing 2.0.0.RELEASE makes springfox die.
springfox' latest version is 2.9.2; even the 3.0.0-SNAPSHOT does not fix the issue: https://github.com/springfox/springfox/issues/2932
Solution
Springfox is apparently abandoned:
Springfox swagger not working in spring boot 2.2.0
Solution is to migrate to a different spring-swagger bridge.
Most likely candidate is Springdoc-openapi:
https://springdoc.github.io/springdoc-openapi-demos/
User experience (last comment from https://github.com/springfox/springfox/issues/2932):
Yes I ended up following the links to 'springDoc' and found a
quick tutorial.
It was super easy to setup all in the pom.xml file.
Just one tweak was needed
<!-- org.springdoc for swagger setup -->
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-core</artifactId>
<version>1.1.49</version>
<exclusions>
<exclusion>
<groupId>io.github.classgraph</groupId>
<artifactId>classgraph</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>1.1.49</version>
</dependency>

javax.persistence jar is not loaded into tomcat

I'm trying to run JPA servlet on Tomcat 8.
From my servlet I call method of a class named QueryDB.class located in another Eclipse project (JPA project, not WEB project).
But tomcat throws
SEVERE: Servlet.service() for servlet [MyRESTService] in context with path [/AutomationWeb] threw exception [org.glassfish.jersey.server.ContainerException: java.lang.NoClassDefFoundError: javax/persistence/Persistence] with root cause
java.lang.ClassNotFoundException: javax.persistence.Persistence
at java.net.URLClassLoader$1.run(Unknown Source)
...
Exception is thrown at line EntityManagerFactory emf = Persistence.createEntityManagerFactory( "AutomationDB_JPA" ); in QueryDB.class
I'm using Eclipse Mars with Maven. I have EclipseLink in my pom.xml defined:
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
<version>2.6.1</version>
</dependency>
I have following jars in my Application's WEB-INF/lib:
eclipselink-2.6.1.jar
javax.persistence-2.1.0.jar , it has javax/persistence/Persistence.class inside
There is no other SEVERE messages in the tomcat log.
What's wrong with this setup?
I also tried adding the persistence jar to apache-tomcat-8.0.28\lib , but got the same error.
Update: I moved Persistence.createEntityManagerFactory call to the servlet itself and got no classloader exception. So the question changes: why classloader used for QueryDB.class is different from servlet's classloader? Also, why classloader used in QueryDB.class does not see the Persistence.class, which is seen by the servlet's classloader?
Ok I've found a solution.
Excluded my JPA project from tomcat VM classpath
Converted my JPA project to the Utility project
Modified Deployment Assembly in my Web project properties so that it
will accept JPA project as .jar archive into WEB-INF/lib
I have no classloader exception anymore.

How to debug NoSuchMethodError exception ? org.hibernate.cfg.Configuration.addAnnotatedClass(Ljava/lang/Class;)

When I try to initialize a Hibernate 4 SessionFactory in a servlet:
Configuration config;
ServiceRegistry registry;
SessionFactory factory;
config = new Configuration();
config = config.addAnnotatedClass(Star.class); // <-- Exception here.
I get an exception:
SEVERE: Servlet.service() for servlet [hu.adamsan.store.TestHibernate] in context with path [/TestProject] threw exception [Servlet execution threw an exception] with root cause
java.lang.NoSuchMethodError: org.hibernate.cfg.Configuration.addAnnotatedClass(Ljava/lang/Class;)Lorg/hibernate/cfg/Configuration;
at hu.adamsan.store.TestHibernate.doGet(TestHibernate.java:75)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at
When I looked up in Google, I could not find a solution, but similar errors were present when there was a mismatch with the hibernate version. I've made a similar project, with the exact same maven dependencies, with the same code initializing hibernate in a doGet method, and it worked.
maven dependencies:
mysql-connector-java 5.1.26
hibernate-core 4.2.6.Final
hibernate-validator 5.0.1.Final
commons-beanutils 1.8.3
commons-collections 3.2.1
log4j 1.2.17
slf4j-api 1.7.5
Does anyone have an idea, what could be wrong?
Can someone offer general advice, how to handle, debug such obscure errors?
When you get a NoSuchMethodError in external libraries, it is usually due to having multiple versions of the same dependency on the classpath. This might be due to a transitive dependency, so detecting it through just looking at the dependency in the pom.xml of your project is not enough. If you do a mvn dependency:tree, it will list all transitive dependencies as well, so look for multiple versions of the same dependency.
It might also be that the same class is implemented is multiple dependencies, so google it to find out which jars contain it. http://www.jarfinder.com/index.php/java/info/org.hibernate.cfg.Configuration