Swagger integration into a NON SpringBoot project using SpringFox - spring

I have the same question as here. I want to integrate swagger into a non spring boot project. I followed the steps given in the answer here which uses springfox but I am getting the following error on server startup:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter': Initialization of bean failed; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'cachingModelPropertiesProvider' defined in URL [jar:file:*/WEB-INF/lib/springfox-schema-2.9.2.jar!/springfox/documentation/schema/property/CachingModelPropertiesProvider.class]: Unsatisfied dependency expressed through constructor parameter 1; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'optimized': Failed to introspect bean class [springfox.documentation.schema.property.OptimizedModelPropertiesProvider] for lookup method metadata: could not find class that it depends on; nested exception is java.lang.NoClassDefFoundError: com/fasterxml/classmate/members/ResolvedParameterizedMember
These are the dependencies added:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
UPDATE:
So I think I was missing a jackson fasterxml dependency. I shuffled around with the pom.xml and the error went away. Still trying to zero in on the exact dependency that was creating the problem.
But though this error went away, another popped up:
org.springframework.context.ApplicationContextException: Failed to start bean 'documentationPluginsBootstrapper'; nested exception is com.google.common.util.concurrent.ExecutionError: java.lang.NoSuchMethodError: com.google.common.collect.FluentIterable.concat(Ljava/lang/Iterable;Ljava/lang/Iterable;)Lcom/google/common/collect/FluentIterable;
So then I switched to the 2.6.1 version of springfox, the second error went away and I got a good clean server start-up.

I think the issue comes from another missing jar, make sure you have two dependency
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.1</version>
<scope>compile</scope>
</dependency>

adding
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>20.0</version>
<exclusions>
<exclusion>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</exclusion>
</exclusions>
</dependency>
solves the nested exception is com.google.common.util.concurrent.ExecutionError: java.lang.NoSuchMethodError: com.google.common.collect.FluentIterable error

Related

Zipkin with Spring Boot 2.2 and Hoxton Cloud

I wanted to update my project Zipkin setup to Spring Boot 2.2.2.RELEASE and Spring Cloud Hoxton.RELEASE but it looks like simple jars update is not enough.
I thought the old setup (it was working fine for Spring Boot 2.1.5.RELEASE and Greenwich.SR2) would also work for Boot 2.2.2.RELEASE and Hoxton.RELEASE but it appears I still miss something here.
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath/>
</parent>
<properties>
...
<spring-cloud.version>Hoxton.RELEASE</spring-cloud.version>
<!-- <zipkin.version>2.12.9</zipkin.version> does not work either-->
<zipkin.version>2.11.8</zipkin.version>
...
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>
<dependency>
<groupId>io.zipkin.java</groupId>
<artifactId>zipkin-server</artifactId>
<version>${zipkin.version}</version>
</dependency>
<dependency>
<groupId>io.zipkin.java</groupId>
<artifactId>zipkin-autoconfigure-ui</artifactId>
<version>${zipkin.version}</version>
<scope>runtime</scope>
</dependency>
...
</dependencies>
I'm getting the following exception (java.lang.NoClassDefFoundError: zipkin2
/internal/Buffer$Writer):
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'webMvcMetricsFilter' defined in class path resource [org/springframework/boot/actuate/autoconfigure/metrics/web/servlet/We
bMvcMetricsAutoConfiguration.class]: Unsatisfied dependency expressed through method 'webMvcMetricsFilter' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'pr
ometheusMeterRegistry' defined in class path resource [org/springframework/boot/actuate/autoconfigure/metrics/export/prometheus/PrometheusMetricsExportAutoConfiguration.class]: Initialization of bean failed; nested exception is or
g.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'zipkin2.server.internal.ZipkinServerConfiguration': Unsatisfied dependency expressed through field 'httpQuery'; nested exception is org
.springframework.beans.factory.BeanCreationException: Error creating bean with name 'zipkin2.server.internal.ZipkinQueryApiV2': Bean instantiation via constructor failed; nested exception is java.lang.NoClassDefFoundError: zipkin2
/internal/Buffer$Writer
It's a version problem. Zipkin officially recommends use zipkin-server instate of customer server after spring-boot 2.x.
For me, the 2.12.9 library works well under spring-boot 2.1.8.RELEASE with spring-cloud Greenwich.SR6

Failed to determine a suitable driver class in Google Cloud Platform

when running my spring-boot app, it throws the following error on google cloud.
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception; nested exception is
Caused by: org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Failed to determine a suitable driver class
my pom.xml :
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.6</version>
</dependency>
<dependency>
<groupId>com.google.cloud.sql</groupId>
<artifactId>mysql-socket-factory-connector-j-6</artifactId>
<version>1.0.10</version>
</dependency>
SELECT VERSION();
output:
5.7.14-google-log
Anybody can help me to resolve this issue?
Use the Spring Cloud GCP Cloud SQL MySQL Starter.
It will bring in the correct dependencies to connect.
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-gcp-starter-sql-mysql</artifactId>
<version>1.0.0.RELEASE</version>
</dependency>

Spring BeanCreationError: java.lang.ArrayStoreException: sun.reflect.annotation.TypeNotPresentExceptionProxy

Is there a way to tell which dependency/annotation is missing in case of this exception?
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'requestMappingHandlerMapping' defined in class path resource [org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Invocation of init method failed; nested exception is java.lang.ArrayStoreException: sun.reflect.annotation.TypeNotPresentExceptionProxy
I'm using spring-boot 1.5.17.RELEASE and spring-cloud Edgware.SR5, as per start.spring.io. I have spring-boot-starter-web, spring-cloud-starter-config and spring-cloud-starter-eureka included. It's a simple jar that's supposed to be deployed on WildFly alongside Keycloak 4.5.0.Final to register it in Netflix's Eureka instance.
I've observed that solution to that problem is always different. Mine was to exclude spring-webmvc from spring-boot-starter-web dependency that I've already had:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</exclusion>
</exclusions>
</dependency>

Spring config server not starting with keystore

trying to boot spring cloud config server with a keystore but am getting the following on startup:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'encryptionBootstrapConfiguration': Unsatisfied dependency expressed through field 'encryptor'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'textEncryptor' defined in org.springframework.cloud.bootstrap.encrypt.EncryptionBootstrapConfiguration$RsaEncryptionConfiguration: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.security.crypto.encrypt.TextEncryptor]: Factory method 'textEncryptor' threw exception; nested exception is java.lang.NullPointerException
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'textEncryptor' defined in org.springframework.cloud.bootstrap.encrypt.EncryptionBootstrapConfiguration$RsaEncryptionConfiguration: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.security.crypto.encrypt.TextEncryptor]: Factory method 'textEncryptor' threw exception; nested exception is java.lang.NullPointerException
Caused by: java.lang.NullPointerException: null
at org.springframework.cloud.context.encrypt.EncryptorFactory.create(EncryptorFactory.java:34) ~[spring-cloud-context-1.1.9.RELEASE.jar:1.1.9.RELEASE]
The following options are used to start the component:
--spring.cloud.config.server.git.uri=http://mygit.com/alexander.lovett/service-discovery-demo.git
--spring.cloud.config.server.git.searchPaths=Software/config
--spring.rabbitmq.host=spring-bus
--management.security.enabled=false
--encrypt.keystore.location=file://C:/Users/607567487/BTTV/workspace/service-discovery-demo/Software/config-serverserver.jks
--encrypt.keystore.secret=changeme
--encrypt.keystore.password=letmein
--encrypt.keystore.alias=mytestkey
I've tried the location as file://C:/.../server.jks file:C:/.../server.jks and even put the jks on the classpath and tried classpath:server.jks
But still get the same error regardless.
My dependencies:
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-monitor</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Camden.SR7</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.5.8.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
Simple fix...
the properties should be encrypt.key-store.* not encrypt.keystore.*
I had the same issue, but a different cause. Here is a short description. In a nutshell, the problem was caused by ENCRYPT_KEY environment variable that triggered textEncryptor bean autoconfig with malformed KeyProperties. Spring failed to populate KeyProperties from the environment variable ENCRYPT_KEY causing NullPointerException. If encryption key is provided as system property -Dencrypt.key=123 then spring will have no problems configuring textEncryptor bean.

Why does Spring Data MongoDB 1.5.2 fail a with NoSuchMethodError?

I can't seem to get the most basic MongoTemplate to initialize with spring-mongodb.
Here's the relevant extract from my POM:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.0.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>1.5.2.RELEASE</version>
</dependency>
My XML context setup looks like this:
<mongo:db-factory id="mongoDbFactory"
host="${mongo.host}"
port="${mongo.port}"
dbname="${mongo.db}"/>
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg name="mongoDbFactory" ref="mongoDbFactory"/>
</bean>
This results in:
java.lang.NoClassDefFoundError: Could not initialize class org.springframework.data.mongodb.core.convert.DefaultMongoTypeMapper
org.springframework.data.mongodb.core.convert.MappingMongoConverter.<init>(MappingMongoConverter.java:104)
org.springframework.data.mongodb.core.MongoTemplate.getDefaultMongoConverter(MongoTemplate.java:1670)
org.springframework.data.mongodb.core.MongoTemplate.<init>(MongoTemplate.java:205)
org.springframework.data.mongodb.core.MongoTemplate.<init>(MongoTemplate.java:191)
...
Debugging the initialization of DefaultMongoTypeMapper reveals:
java.lang.NoSuchMethodError: org.springframework.data.util.ClassTypeInformation.from(Ljava/lang/Class;)Lorg/springframework/data/util/TypeInformation;
I've tried all sorts of different version combinations for spring-core and spring-data, as well as trying to manually specify a version of spring-data-commons, no dice.
Is there something basic I'm doing wrong here?
EDIT: Full trace of error is:
Error creating bean with name 'mongoTemplate' defined in ServletContext resource [/WEB-INF/mvc-dispatcher-servlet.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.data.mongodb.core.MongoTemplate]: Constructor threw exception; nested exception is java.lang.NoSuchMethodError: org.springframework.data.util.ClassTypeInformation.from(Ljava/lang/Class;)Lorg/springframework/data/util/TypeInformation;
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:288)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1120)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:522)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:461)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:607)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:647)
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:598)
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:661)
at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:517)
at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:458)
at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:138)
As indicated in the ticket you filed, Spring Data MongoDB 1.5.2 requires Spring Data Commons 1.8.2 (or any other 1.8.x version). The Spring version has to be at least 3.2.10 (although Spring itself doesn't play into the exception you see as it is about a tiny signature change in Spring Data Common's ClassTypeInformation). If that's the case, you won't see the original exception.
We generally recommend to use the Spring Data Releasetrain BOM or the Spring IO platform instead of manually dealing with versions.
Try making your dependencies like this ;). You might be missing something from the tutorial page.
<dependencies>
<!-- Spring framework -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.2.2.RELEASE</version>
</dependency>
<!-- mongodb java driver -->
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>2.11.0</version>
</dependency>
<!-- Spring data mongodb -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>1.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.2.2</version>
</dependency>
</dependencies>
source: http://www.mkyong.com/mongodb/spring-data-mongodb-hello-world-example/
OK.. so it seems there is an incompatibility somewhere. It's unclear but the stack trace seems to indicate that AutowiredAnnotationBeanPostProcessor is trying to call a method of ClassTypeInformation that doesn't exist. This method was last seen in 1.7.3-RELEASE of spring-data-commons. Reverting to that version appears to fix things, but I'm not sure if this will cause errors elsewhere.
The final POM looks like this:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.0.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
<version>1.7.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>1.5.2.RELEASE</version>
</dependency>
Try including spring-data-commons Jar. This class is defined here. I guess this is why it is causing the issue.

Resources