Flyway configuration without datasource - spring-boot

Try to setup Flyway v6.0.8 with Spring Boot v2.2.1.RELEASE in application.yaml file:
spring:
# CockroachDB connection
r2dbc:
url: r2dbc:postgresql://localhost:26257/defaultdb
username: test_user
password: qwerty
schema: some_schema
flyway:
# Doesn't support r2dbc driver, and don't think it's really necessary just for database structuring(will be no actual data migrations)
url: jdbc:postgresql://localhost:26257/defaultdb
# schemas: some_schema
user: test_user
password: qwerty
I want to configure Flyway just using spring.flyway.url/user/password without defining any spring.datasource in addition. As I understand that's possible to do based on this merged PR but I still have faced with the following exception regarding not defined datasource:
Caused by: Error creating bean with name 'flyway' defined in class path resource [org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration$FlywayConfiguration.class]: Unsatisfied dependency expressed through method 'flyway' parameter 1; nested exception org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'spring.datasource-org.springframework.boot.autoconfigure.jdbc.DataSourceProperties': Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.autoconfigure.jdbc.DataSourceProperties]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: org/springframework/jdbc/datasource/embedded/EmbeddedDatabaseType
What I do wrong?
build.gradle
plugins {
id 'org.flywaydb.flyway' version '6.1.1'
id 'org.springframework.boot' version '2.2.1.RELEASE'
}
apply plugin: 'io.spring.dependency-management'
repositories {
mavenCentral()
maven { url = uri("https://repo.spring.io/milestone") }
}
ext {
r2dbcPostgresqlVersion = '0.8.0.RELEASE'
postgresqlVersion = '42.2.9'
}
dependencies {
implementation("org.springframework.boot.experimental:spring-boot-starter-data-r2dbc")
implementation('org.springframework.boot:spring-boot-starter-webflux')
implementation('org.flywaydb:flyway-core')
implementation("io.r2dbc:r2dbc-postgresql:${r2dbcPostgresqlVersion}")
implementation("org.postgresql:postgresql:${postgresqlVersion}")
compileOnly('org.projectlombok:lombok')
annotationProcessor('org.projectlombok:lombok')
annotationProcessor('org.springframework.boot:spring-boot-configuration-processor')
testImplementation('org.springframework.boot:spring-boot-starter-test')
testImplementation('io.projectreactor:reactor-test')
}
dependencyManagement {
imports {
mavenBom("org.springframework.boot.experimental:spring-boot-bom-r2dbc:0.1.0.M3")
}
}

Related

GraphQLName annotation

I am creating a simple login app using spring boot with Kotlin + GraphQL. #GraphQLName("") is not working for me.
my graphql is
schema {
query: Query
mutation: Mutation
}
type Mutation{
requestOTP(credential: String!,authType: String!): String
}
and in my kotlin
#Controller
class ResetPassword(
#Autowired val userRepository: UserRepository
): GraphQLMutationResolver {
#GraphQLName("requestOTP")
fun reset(credential: String, authType: String): String{
//codes...
}
}
But It keep saying
Error starting Tomcat context. Exception: org.springframework.beans.factory.UnsatisfiedDependencyException. Message:
Error creating bean with name 'graphQLServletRegistrationBean' defined in class path resource [graphql/kickstart/autoconfigure/web/servlet/GraphQLWebAutoConfiguration.class]: Unsatisfied dependency expressed through method 'graphQLServletRegistrationBean' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'graphQLHttpServlet' defined in class path resource [graphql/kickstart/autoconfigure/web/servlet/GraphQLWebAutoConfiguration.class]: Unsatisfied dependency expressed through method 'graphQLHttpServlet' parameter 0;
nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'graphQLServletConfiguration' defined in class path resource [graphql/kickstart/autoconfigure/web/servlet/GraphQLWebAutoConfiguration.class]: Unsatisfied dependency expressed through method 'graphQLServletConfiguration' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'invocationInputFactory' defined in class path resource [graphql/kickstart/autoconfigure/web/servlet/GraphQLWebAutoConfiguration.class]:
Unsatisfied dependency expressed through method 'invocationInputFactory' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'graphQLSchemaProvider' defined in class path resource [graphql/kickstart/autoconfigure/web/servlet/GraphQLWebAutoConfiguration.class]:
Unsatisfied dependency expressed through method 'graphQLSchemaProvider' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'graphQLSchema' defined in class path resource [graphql/kickstart/autoconfigure/tools/GraphQLJavaToolsAutoConfiguration.class]: Unsatisfied dependency expressed through method 'graphQLSchema' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'schemaParser' defined in class path resource [graphql/kickstart/autoconfigure/tools/GraphQLJavaToolsAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [graphql.kickstart.tools.SchemaParser]: Factory method 'schemaParser' threw exception; nested exception is graphql.kickstart.tools.resolver.FieldResolverError: No method found as defined in schema <unknown>:44 with any of the following signatures (with or without one of [interface graphql.schema.DataFetchingEnvironment, class graphql.GraphQLContext] as the last argument), in priority order:
com.auth.test.presentation.ResetPassword.requestOTP(~credential, ~authType)
com.auth.test.presentation.ResetPassword.getRequestOTP(~credential, ~authType)
If I rename fun reset to fun requestOTP, the code compile even I put #GraphQLName("LOLnothingLOL")
. Kindly explain me why I am getting this error and how to fix it. I don't want to use requestOTP directly to have more readability.
Edit:
I am not using pom.xml instead build.gradle
Here is my gradle file.
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
id("org.springframework.boot") version "2.7.6"
id("io.spring.dependency-management") version "1.1.0"
kotlin("jvm") version "1.7.21"
kotlin("plugin.spring") version "1.7.21"
kotlin("plugin.jpa") version "1.7.21"
}
group = "com.dagger"
version = "0.0.1"
java.sourceCompatibility = JavaVersion.VERSION_11
repositories {
mavenCentral()
}
dependencies {
implementation("org.springframework.boot:spring-boot-starter-data-jpa")
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("org.springframework.boot:spring-boot-starter-graphql")
implementation("com.graphql-java-kickstart:graphql-spring-boot-starter:14.1.0")
runtimeOnly("com.graphql-java-kickstart:graphiql-spring-boot-starter:11.1.0")
implementation("org.springframework.boot:spring-boot-starter-oauth2-client")
implementation("org.springframework.boot:spring-boot-starter-oauth2-resource-server")
implementation("org.springframework.boot:spring-boot-starter-security")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
implementation ("org.springframework.boot:spring-boot-starter-log4j2")
modules {
module("org.springframework.boot:spring-boot-starter-logging") {
replacedBy("org.springframework.boot:spring-boot-starter-log4j2", "Use Log4j2 instead of Logback")
}
}
//jwt token
implementation ("io.jsonwebtoken:jjwt:0.9.1")
implementation("com.expedia.graphql:graphql-kotlin-schema-generator:1.3.4")
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
implementation("org.springframework.session:spring-session-core")
runtimeOnly("com.mysql:mysql-connector-j")
testImplementation("org.springframework.boot:spring-boot-starter-test")
testImplementation("org.springframework:spring-webflux")
testImplementation("org.springframework.graphql:spring-graphql-test")
testImplementation("org.springframework.security:spring-security-test")
}
tasks.withType<KotlinCompile> {
kotlinOptions {
freeCompilerArgs = listOf("-Xjsr305=strict")
jvmTarget = "11"
}
}
tasks.withType<Test> {
useJUnitPlatform()
}
Let's try to understand the issue.
Unsatisfied dependency expressed through method 'graphQLSchemaProvider' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'graphQLSchema' defined in class path resource [graphql/kickstart/autoconfigure/tools/GraphQLJavaToolsAutoConfiguration.class]: Unsatisfied dependency expressed through method 'graphQLSchema' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'schemaParser' defined in class path resource [graphql/kickstart/autoconfigure/tools/GraphQLJavaToolsAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [graphql.kickstart.tools.SchemaParser]: Factory method 'schemaParser' threw exception; nested exception is graphql.kickstart.tools.resolver.FieldResolverError: No method found as defined in schema <unknown>:44 with any of the following signatures (with or without one of [interface graphql.schema.DataFetchingEnvironment, class graphql.GraphQLContext] as the last argument), in priority order:
In above error message if you notice last statement, issue is very clear.
nested exception is graphql.kickstart.tools.resolver.FieldResolverError: No method found as defined in schema <unknown>:44 with any of the following signatures (with or without one of [interface graphql.schema.DataFetchingEnvironment, class graphql.GraphQLContext] as the last argument), in priority order:
So the issue is, in your Controller class, Mutation method name is reset() but in GraphQl schema file method name is requestOTP(). Both should be same.
schema {
query: Query
mutation: Mutation
}
type Mutation{
requestOTP(credential: String!,authType: String!): String
}
Also I can see you are using org.springframework.boot:spring-boot-starter-graphql dependency in your build.gradle file. This dependency is enough for GraphQL implementation with Spring Boot. Please remove other GraphQL related dependencies. They are creating confusion in Spring ECO system.
Also if you writing GraphQL schema file yourself then no need to use #GraphQLName annotation.

Error creating bean with name org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaConfiguration (Using Gradle)

Building jar file works through the terminal computer path % ./gradlew build, and application runs computer path % java -jar name.jar.
Why not through IntelliJ?
*Secondary Issue -
When I try to upload the jar file built through the terminal to AWS Beanstalk I get a validation error.
*Primary Issue -
The application runs in IntelliJ.
After I have completed the Project Structure setup, and the Build Artifacts execution. The new .jar file fails to run,
throwing the exception below.
Error creating bean with name 'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaConfiguration': Unsatisfied dependency expressed through constructor 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 org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Failed to determine a suitable driver class
build.gradle:
plugins {
id 'org.springframework.boot' version '2.6.2'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
id 'application'
}
group = 'com.example'
version = 'api'
sourceCompatibility = '17'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-validation'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
runtimeOnly 'mysql:mysql-connector-java'
}
test {
useJUnitPlatform()
}
mainClassName = 'com.example.name.applicationame'
task fatJar(type: Jar) {
bootJar {
launchScript()
}
manifest {
attributes 'Main-Class': "${mainClassName}"
duplicatesStrategy = 'include'
}
archiveBaseName = "name"
from { configurations.compileClasspath.collect { it.isDirectory() ? it : zipTree(it) } }
with jar
}
application.properties:
spring.datasource.url=jdbc:mysql://localhost:3306/name?useSSL=true
spring.datasource.username=root
spring.datasource.password=password
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.database-platform=org.hibernate.dialect.MySQL8Dialect
spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
Thanks!

spring-cloud-stream sample projects raises NoSuchBeanDefinitionException of KafkaStreamsFunctionProcessor

I'm trying to touch spring-cloud-stream, and creating a sample project of the official blog.
Implementation is totally same as the article.
#SpringBootApplication
public class SimpleConsumerApplication {
#Bean
public java.util.function.Consumer<KStream<String, String>> process() {
return input ->
input.foreach((key, value) -> {
System.out.println("Key: " + key + " Value: " + value);
});
}
}
I've selected Cloud Stream and Spring for Apache Kafka Stream on Spring initializr, and added ShadowJar. Now my build.gradle is like this.
plugins {
id 'org.springframework.boot' version '2.4.4'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
id 'com.github.johnrengelman.shadow' version '6.1.0'
}
group = 'com.lipsum'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
jar {
manifest {
attributes('Main-Class': 'com.lipsum.kafkastream.KafkastreamApplication')
}
}
shadowJar {
archiveBaseName.set('kafka-stream-practice')
archiveClassifier.set('')
archiveVersion.set('')
}
repositories {
mavenCentral()
}
ext {
set('springCloudVersion', "2020.0.2")
}
dependencies {
implementation 'org.apache.kafka:kafka-streams'
implementation 'org.springframework.cloud:spring-cloud-stream'
implementation 'org.springframework.cloud:spring-cloud-stream-binder-kafka-streams'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}
}
test {
useJUnitPlatform()
}
I execute the uber jar, but springboot application fails to recognize the bean.
$ java -jar kafka-stream-practice.jar --spring.cloud.stream.bindings.process-in-0.destination=kafka-stream-practice
...
22:47:21.162 [main] ERROR org.springframework.boot.SpringApplication - Application run failed
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'kafkaStreamsFunctionProcessorInvoker' defined in class path resource [org/springframework/cloud/stream/binder/kafka/streams/function/KafkaStreamsFunctionAutoConfiguration.class]: Unsatisfied dependency expressed through method 'kafkaStreamsFunctionProcessorInvoker' parameter 1; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.cloud.stream.binder.kafka.streams.KafkaStreamsFunctionProcessor' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.cloud.stream.binder.kafka.streams.KafkaStreamsFunctionProcessor' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
I don't think the implementation has any problems. Do I miss some dependencies?
I tried a quick maven project from the initialize and it starts fine. There was a known bug on the last release (3.0.11) which was fixed since then on the snapshot. You can fix the issue by adding the boot actuator dependency to the project or by upgrading the binder to the latest snapshot. Could you try the maven approach? If the problem still persists, please share a reproducible sample, and then we will take a look.
It works after removing shadowJar and instead uses bootJar task of Spring Boot Gradle plugin.

Spring boot application gives "unable to start tomcat" exception with spring-boot-starter-actuator

I had my spring-boot application, with spring-boot-starter-web, then I added spring-boot-starter-actuator to gradle file. There is no compilation error. But when i try to run the server it gives the following exception.
my build.gradle content (only the main ones included )
plugins {
id 'org.springframework.boot' version '2.1.3.RELEASE'
} ...........
compile "org.springframework.boot:spring-boot-starter-actuator"
compile group: 'io.micrometer', name: 'micrometer-registry-prometheus', version: '1.1.3' .........
my application.properties content
server.port = 2128
spring.couchbase.env.timeouts.connect=10000
spring.couchbase.env.timeouts.query=180000
spring.couchbase.env.timeouts.view=20000
spring.couchbase.bootstrap-hosts=localhost
spring.couchbase.bucket.name=localdb
spring.couchbase.bucket.password=*****
spring.data.couchbase.repositories.type=auto
spring.data.couchbase.auto-index=true
server.compression.enabled=true
server.compression.mime-types=application/json,text/plain
spring.servlet.multipart.enabled=true
spring.servlet.multipart.maxFileSize=50MB
spring.servlet.multipart.maxRequestSize=50MB
my Application.java
#SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Gives the following exception while starting the server.
org.springframework.context.ApplicationContextException: Unable to start web server;
nested exception is org.springframework.boot.web.server.WebServerException: Unable to start embedded Tomcat
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'servletEndpointRegistrar' defined in class path resource
WebMvcServletEndpointManagementContextConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException:
Failed to instantiate [org.springframework.boot.actuate.endpoint.web.ServletEndpointRegistrar]: Factory method 'servletEndpointRegistrar' threw exception;
nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'healthEndpoint' defined in class path resource
org/springframework/boot/actuate/autoconfigure/health/HealthEndpointConfiguration.class]: Unsatisfied dependency expressed through method 'healthEndpoint' parameter 1;
nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'healthIndicatorRegistry' defined in class path resource [org/springframework/boot/actuate/autoconfigure/health/HealthIndicatorAutoConfiguration.class]: Bean instantiation via factory method failed
Any helpful hints?
I was finally able to start the application after I turned off default health checks via the following entry in application.properties file
management.health.defaults.enabled=false

spring boot: can't initialize a postgresql datasource

I have the following bean configured.
#Bean
#Primary
#ConfigurationProperties(prefix="datasource.etlTarget")
public DataSource datasourceTest() {
System.out.println("************");
System.out.println("************");
return DataSourceBuilder.create().build();
}
Then error log is:
************
************
2014-12-14 21:12:35.718 WARN 21758 --- [ main] s.c.a.AnnotationConfigApplicationContext : Exception encountered during context initialization - cancelling refresh attempt
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.batch.core.configuration.annotation.SimpleBatchConfiguration': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private java.util.Collection org.springframework.batch.core.configuration.annotation.AbstractBatchConfiguration.dataSources; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'datasourceTest' defined in class path resource [com/testcom/etc/ETLJobRepository.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.sql.DataSource]: Factory method 'datasourceTest' threw exception; nested exception is java.lang.IllegalStateException: No supported DataSource type found
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
Key error message is
nested exception is java.lang.IllegalStateException: No supported DataSource type found
The gradle setting is
dependencies {
compile "org.springframework.boot:spring-boot-starter-batch:1.2.0.RELEASE"
compile "org.springframework:spring-jdbc:4.1.3.RELEASE"
compile 'org.postgresql:postgresql:9.3-1102-jdbc41'
testCompile group: 'junit', name: 'junit', version: '4.11'
}
I tried hard to figure it out why it error out, but don't find out the root cause
The property is
datasource.etlTarget.url=jdbc:postgresql://localhost/example
datasource.etlTarget.driverClassNname=org.postgresql.Driver
datasource.etlTarget.username=postgres
datasource.etlTarget.password=test
You made a typo in your configuration:
datasource.etlTarget.driverClassNname=org.postgresql.Driver
Should be:
datasource.etlTarget.driverClassName=org.postgresql.Driver
(i.e. driverClassName instead of driverClassNname)

Resources