Am new to ElasticSearch...
Really love the API (especially ElasticsearchTemplate & supporting Unit Tests)...
Was following this example using ElasticSearch 5.5.0 (the following link has the full code inline and also available as a downloadable zip file):
https://www.mkyong.com/spring-boot/spring-boot-spring-data-elasticsearch-example/
maven dependencies:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.1.RELEASE</version>
</parent>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
EsConfig:
#Configuration
#EnableElasticsearchRepositories(basePackages = "com.mkyong.book.repository")
public class EsConfig {
#Value("${elasticsearch.host}")
private String EsHost;
#Value("${elasticsearch.port}")
private int EsPort;
#Value("${elasticsearch.clustername}")
private String EsClusterName;
#Bean
public Client client() throws Exception {
Settings esSettings = Settings.settingsBuilder()
.put("cluster.name", EsClusterName)
.build();
//https://www.elastic.co/guide/en/elasticsearch/guide/current/_transport_client_versus_node_client.html
return TransportClient.builder()
.settings(esSettings)
.build()
.addTransportAddress(
new InetSocketTransportAddress(InetAddress.getByName(EsHost), EsPort));
}
#Bean
public ElasticsearchOperations elasticsearchTemplate() throws Exception {
return new ElasticsearchTemplate(client());
}
}
src/main/resources:
elasticsearch.clustername = mkyong-cluster
elasticsearch.host = localhost
elasticsearch.port = 9300
When issuing the following, either:
mvn spring-boot:run
or
mvn clean test
stdout:
Caused by: NoNodeAvailableException[None of the configured nodes are available: [{#transport#-1}{127.0.0.1}{127.0.0.1:9300}]]
at org.elasticsearch.client.transport.TransportClientNodesService.ensureNodesAreAvailable(TransportClientNodesService.java:326)
at org.elasticsearch.client.transport.TransportClientNodesService.execute(TransportClientNodesService.java:223)
at org.elasticsearch.client.transport.support.TransportProxyClient.execute(TransportProxyClient.java:55)
Also, get the following in my ElasticSearch 5.5.0 engine's stdout:
[2017-07-23T02:48:46,135][WARN ][o.e.t.n.Netty4Transport ] [vY7jxpr] exception caught on transport layer [[id: 0xa7e950be, L:/127.0.0.1:9300 - R:/127.0.0.1:60190]], closing connection
java.lang.IllegalStateException: Received message from unsupported version: [1.0.0] minimal compatible version is: [5.0.0]
at org.elasticsearch.transport.TcpTransport.messageReceived(TcpTransport.java:1379) ~[elasticsearch-5.5.0.jar:5.5.0]
at org.elasticsearch.transport.netty4.Netty4MessageChannelHandler.channelRead(Netty4MessageChannelHandler.java:74) ~[transport-netty4-5.5.0.jar:5.5.0]
Is there any way to use Spring Data ElasticSearch with ElasticSearch 5.5.0 engine?
If so, how would the connection be implemented?
I am using the Elastic Search Java client provided by Elastic Search and am able to connect and create indices on ElasticSearch 5.5.0 but really wish I could use the power of this library.
Spring Boot 1.5.1 does not support ElasticSearch 5.x (the article you provided also says it). To make it possible to work with ElasticSearch 5.x you need to use a milestone of Spring Boot 2.
Try to configure your project with following dependencies:
# spring boot
compile 'org.springframework.boot:spring-boot:2.0.0.M2'
# elasticsearch
compile 'org.elasticsearch:elasticsearch:5.5.0'
compile 'org.elasticsearch.client:transport:5.5.0'
# spring-data-elasticsearch for spring boot
compile 'org.springframework.boot:spring-boot-starter-data-elasticsearch:2.0.0.M2'
This should allow you to use all the goodies of Elasticsearch Java API ans spring-data-elasticsearch.
Update: Basic Gradle (v4.0.1) configuration would look like (build.gradle file):
buildscript {
ext {
springBootVersion = '2.0.0.M2'
}
repositories {
mavenCentral()
maven { url 'https://repo.spring.io/libs-snapshot' }
maven { url 'http://repo.spring.io/milestone/' }
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
apply plugin: 'java-library'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
jar {
baseName = 'elastic'
version = '0.0.1'
}
repositories {
mavenCentral()
jcenter()
maven { url 'https://repo.spring.io/libs-snapshot' }
maven { url 'http://repo.spring.io/milestone/' }
}
dependencies {
compile 'org.springframework.boot:spring-boot-starter-web'
compile 'org.springframework.boot:spring-boot-starter-logging'
compile 'org.springframework.boot:spring-boot-starter-data-elasticsearch'
compile 'org.elasticsearch:elasticsearch:5.4.1'
compile 'org.elasticsearch.client:transport:5.4.1'
compile 'com.google.guava:guava:20.0'
testCompile 'junit:junit:4.12'
}
Related
I'm trying to upgrade a project from SpringCloud BOM 2020.0.1 to 2020.0.2
When I change the BOM and re-build, I get an error in JUnit tests, saying that the new parameter spring.config.import is not set for configserver.
This project is not a ConfigServer, neither use ConfigServer (commented config client)
This is the reported error in tests contextLoads()
java.lang.IllegalStateException: Failed to load ApplicationContext
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:132)
at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:124)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:118)
.. Many more
Caused by: org.springframework.cloud.config.client.ConfigServerConfigDataMissingEnvironmentPostProcessor$ImportException: No spring.config.import set
at org.springframework.cloud.config.client.ConfigServerConfigDataMissingEnvironmentPostProcessor.postProcessEnvironment(ConfigServerConfigDataMissingEnvironmentPostProcessor.java:60)
at org.springframework.boot.env.EnvironmentPostProcessorApplicationListener.onApplicationEnvironmentPreparedEvent(EnvironmentPostProcessorApplicationListener.java:100)
at org.springframework.boot.env.EnvironmentPostProcessorApplicationListener.onApplicationEvent(EnvironmentPostProcessorApplicationListener.java:86)
... Many more
This is my build.gradle
plugins {
id 'org.springframework.boot' version '2.4.2'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
}
group = 'com.example.microservices.composite.product'
version = '1.0.0-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
maven {
url 'https://repo.spring.io/milestone'
}
}
ext {
// resilience4jVersion = "1.7.0"
resilience4jVersion = "1.6.1"
}
dependencies {
// Local projects dependencies
implementation project(':api')
implementation project(':util')
// Implementations dependencies
// Standard (actuator - for monitoring and Health)
implementation 'org.springframework.boot:spring-boot-starter-actuator'
// WebFlux (asynchronous Web)
implementation 'org.springframework.boot:spring-boot-starter-webflux'
// SpringFox dependencies
implementation "io.springfox:springfox-boot-starter:3+"
implementation('io.springfox:springfox-spring-webflux:3+')
// Implementation: Spring cloud
implementation('org.springframework.cloud:spring-cloud-starter-config')
implementation('org.springframework.cloud:spring-cloud-starter-stream-rabbit')
implementation('org.springframework.cloud:spring-cloud-starter-stream-kafka')
// Security
implementation('org.springframework.boot:spring-boot-starter-security')
implementation('org.springframework.security:spring-security-oauth2-resource-server')
implementation('org.springframework.security:spring-security-oauth2-jose')
// CircuitBreaker with Resilience4J
implementation("io.github.resilience4j:resilience4j-spring-boot2:${resilience4jVersion}")
implementation("io.github.resilience4j:resilience4j-reactor:${resilience4jVersion}")
// Implementation: Tracing
implementation('org.springframework.cloud:spring-cloud-starter-sleuth')
// Implementation: Performance metrics
implementation("io.micrometer:micrometer-registry-prometheus")
// TEST dependencies
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
testImplementation 'io.projectreactor:reactor-test'
testImplementation('org.springframework.cloud:spring-cloud-stream-test-support')
}
dependencyManagement {
imports {
// mavenBom 'org.springframework.cloud:spring-cloud-dependencies:2020.0.1'
mavenBom "org.springframework.cloud:spring-cloud-dependencies:2020.0.2"
}
}
test {
useJUnitPlatform()
}
And my contextLoads() method in test class is trivial
// Test: Application
#AutoConfigureWebTestClient
#SpringBootTest(
webEnvironment = WebEnvironment.RANDOM_PORT,
classes = {
ProductCompositeServiceApplication.class,
TestSecurityConfig.class },
properties = {
"spring.main.allow-bean-definition-overriding=true" })
#Test
public void contextLoads() {
}
}
NOTE: I have also tried defining the `spring.config.import' property to empty or none in the class, with no change
#SpringBootTest(
webEnvironment = WebEnvironment.RANDOM_PORT,
classes = {
ProductCompositeServiceApplication.class,
TestSecurityConfig.class },
properties = {
"spring.main.allow-bean-definition-overriding=true",
"spring.config.import=" })
I've faced same issue and resolved by adding bootstrap lib with config lib as follows,
implementation 'org.springframework.cloud:spring-cloud-starter-config'
implementation 'org.springframework.cloud:spring-cloud-starter-bootstrap'
I have noted the same problem after upgrading to SpringCloud 2020.0.2
Adding spring.cloud.config.enabled=false in the tests solved the issue.
E.g.:
#SpringBootTest(
webEnvironment = RANDOM_PORT,
properties = {"spring.cloud.config.enabled=false"}
)
i fixed this. adding the dependency
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
You can disable the import checks by adding these lines to your application.yml file in the test/resources folder:
spring:
cloud:
config:
import-check:
enabled: false
Temporary solution
spring.cloud.config.import-check.enabled=false
This method work for me:
<dependency>
<groupId>org.spring framework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
Please check your application.yml or application.xml (which is your configuration file is in place and it is able to pull all the configuration
Our application is currently running on spring boot 1.5.10.RELEASE version, and I'm trying to upgrade to spring boot 2.0.0.RELEASE But after upgrading to 2.0.0.RELEASE I get the following error
Error: Could not find or load main class com.app.MyApplication
Given Below is the build.gradle file
buildscript {
ext {
springBootVersion = '2.0.0.RELEASE'
camelVersion = '2.24.0'
}
repositories {
mavenCentral()
jcenter()
maven { url "https://plugins.gradle.org/m2/" }
maven { url "http://repo.spring.io/snapshot" }
maven { url "http://repo.spring.io/milestone" }
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
apply from: '../common.gradle'
apply from: '../test.gradle'
apply plugin: 'org.springframework.boot'
apply plugin: 'war'
apply plugin: 'io.spring.dependency-management'
dependencies {
providedRuntime "org.springframework.boot:spring-boot-starter-tomcat"
compile ("org.flywaydb:flyway-core:5.0.7")
compile ("org.springframework.retry:spring-retry:1.2.2.RELEASE")
compile("org.springframework:spring-jms:5.1.1.RELEASE")
compile("org.springframework.boot:spring-boot-starter-activemq:2.0.6.RELEASE")
compile("org.springframework:spring-jdbc:5.0.4.RELEASE")
compile("org.springframework.retry:spring-retry:1.2.2.RELEASE")
compile("com.amazonaws:aws-java-sdk-sqs:1.11.428")
compile("com.amazonaws:amazon-sqs-java-messaging-lib:1.0.4")
compile ("org.flywaydb:flyway-core:5.0.7")
runtime("org.apache.activemq:activemq-kahadb-store:5.15.6")
compile("org.apache.camel:camel-spring-boot-starter:${camelVersion}")
compile("org.apache.camel:camel-core:${camelVersion}")
compile("org.apache.camel:camel-http-starter:${camelVersion}")
compile("org.apache.camel:camel-http4:${camelVersion}")
compile("org.apache.camel:camel-jsonpath:${camelVersion}")
compile("org.apache.camel:camel-jackson:${camelVersion}")
compile("org.apache.camel:camel-base64:${camelVersion}")
compile("org.apache.camel:camel-groovy:${camelVersion}")
compile("org.apache.camel:camel-jolt:${camelVersion}")
compile("org.apache.camel:camel-jaxb:${camelVersion}")
compile("org.apache.camel:camel-ahc:${camelVersion}")
compile("org.codehaus.groovy:groovy-json:3.0.3")
}
configurations {
all*.exclude group: '', module: 'servlet-api'
}
springBoot {
mainClassName = 'com.app.MyApplication'
}
war {
archiveName = "application.war"
}
I'm wondering if this is due to the camel dependencies as when I tried to use Spring Boot version 1.5.22. It is working fine but as soon as I change this to 2.x.x it stop working.
This issue got resolved when I ran this application in Intellij instead of the eclipse.
I'm having problems for IntelliJ to pickup custom spring configuration metadata with Gradle.
If I create a new Spring Boot project with the Initializer, include the Configuration Processor in the dependencies, on the Gradle task set the following tasks,
create a class with the content:
package com.example.demo;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
#Component
#ConfigurationProperties("mycustomconfig")
public class MyCustomConfig {
private String name;
public String getName() {
return name;
}
public MyCustomConfig setName(String name) {
this.name = name;
return this;
}
}
then IntelliJ complains in the class file "Spring Boot Configuration Annotation Processor not found in classpath", even though it is definitely on the classpath.
After running the application, there is a file generated in build/classes/java/main/META-INF/spring-configuration-metadata.json with the following content:
{
"groups": [
{
"name": "mycustomconfig",
"type": "com.example.demo.MyCustomConfig",
"sourceType": "com.example.demo.MyCustomConfig"
}
],
"properties": [
{
"name": "mycustomconfig.name",
"type": "java.lang.String",
"sourceType": "com.example.demo.MyCustomConfig"
}
],
"hints": []
}
But IntelliJ then complains in application.properties: Cannot resolve configuration property "mycustomconfig.name".
The same experiment works flawlessly with Maven. Is there anything I'm doing wrong?
I'm using IntelliJ 2018.3 Ultimate.
My build.gradle is:
plugins {
id 'org.springframework.boot' version '2.1.3.RELEASE'
id 'java'
}
apply plugin: 'io.spring.dependency-management'
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter'
annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
Finally, I've found the cause of the issue.
The annotation processor outputs the spring-configuration-metadata.json to build/classes/java/main/META-INF`.
But: IntelliJ uses a different classpath for the resolvement. Going to Project structure/Modules/main module/Paths, you can see that for the compiler output is set to "use module compile output path" and points to out/production/classes. This is resolved from Gradle automatically; changing it will be reverted once you have any changes in Gradle.
I've found that there are two possibilites:
Configure the Spring Boot Annotation Processor manually in IntelliJ Preferences/Build, Execution, Deployment/Compiler/Annotation Processors, with the following settings:
This has the benefit, that you do not need to run a complete gradle build - Just compiling from IntelliJ works. Unfortunately, every user in the project seems to manually set this up.
The second possiblity is mentioned at at this Stack overflow question. Set this idea options in Gradle:
idea{
module{
inheritOutputDirs = false
outputDir = compileJava.destinationDir
testOutputDir = compileTestJava.destinationDir
}
}
This basically now uses one compiled target class both for IntelliJ as well as Gradle. There seem to be some caveats, though, as mentioned in the linked urls.
Not sure if you solved this but I just upgraded to 2018.3 Ultimate and ran into the same problem. I suspect it is an IntelliJ issue but regardless I solved it by adding the following line
implementation('org.springframework.boot:spring-boot-configuration-processor')
Just above the "annotationProcessor" line in my gradle file...
Hope that helps
If you are using Maven, adding this dependency would resolve the issue.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
I use Spring Boot and Spring Security in combination with Keycloak. The build tool is gradle.
When I run ./gradlew bootRun the application works flawless. If I use the resulting fat jar (i.e. java -jar myapp.jar) the application will boot but I encounter an exception when the application tries to invoke some keyloak stuff:
java.lang.IllegalArgumentException: org.keycloak.admin.client.resource.RealmsResource referenced from a method is not visible from class loader
at java.base/java.lang.reflect.Proxy$ProxyBuilder.ensureVisible(Proxy.java:851) ~[na:na]
at java.base/java.lang.reflect.Proxy$ProxyBuilder.validateProxyInterfaces(Proxy.java:682) ~[na:na]
at java.base/java.lang.reflect.Proxy$ProxyBuilder.<init>(Proxy.java:628) ~[na:na]
at java.base/java.lang.reflect.Proxy.lambda$getProxyConstructor$1(Proxy.java:426) ~[na:na]
at java.base/jdk.internal.loader.AbstractClassLoaderValue$Memoizer.get(AbstractClassLoaderValue.java:327) ~[na:na]
at java.base/jdk.internal.loader.AbstractClassLoaderValue.computeIfAbsent(AbstractClassLoaderValue.java:203) ~[na:na]
at java.base/java.lang.reflect.Proxy.getProxyConstructor(Proxy.java:424) ~[na:na]
at java.base/java.lang.reflect.Proxy.newProxyInstance(Proxy.java:999) ~[na:na]
at org.jboss.resteasy.client.jaxrs.ProxyBuilder.proxy(ProxyBuilder.java:79) ~[resteasy-client-3.1.4.Final.jar!/:3.1.4.Final]
at org.jboss.resteasy.client.jaxrs.ProxyBuilder.build(ProxyBuilder.java:131) ~[resteasy-client-3.1.4.Final.jar!/:3.1.4.Final]
at org.jboss.resteasy.client.jaxrs.internal.ClientWebTarget.proxy(ClientWebTarget.java:93) ~[resteasy-client-3.1.4.Final.jar!/:3.1.4.Final]
at org.keycloak.admin.client.Keycloak.realms(Keycloak.java:114) ~[keycloak-admin-client-3.4.3.Final.jar!/:3.4.3.Final]
at org.keycloak.admin.client.Keycloak.realm(Keycloak.java:118) ~[keycloak-admin-client-3.4.3.Final.jar!/:3.4.3.Final]
So I figured out that there must be something wrong how I start the application in the terminal. I found this official site, which explains how to run an Spring application from terminal. So I tried all of the solutions including:
$ unzip -q myapp.jar
$ java org.springframework.boot.loader.JarLauncher
but I get the same error.
After 2 days of searching and experimenting I'm out of any ideas.
So my question is basically:
Does anyone have any idea how to solve this problem?
My knowledge regarding the classloader is also limited - so any pratical hints in this direction are very welcomed as well.
EDIT (added build.gradle):
There were some Jackson problems that's why the Spring web dependencies are included directly (without the starter and therefor without Jackson).
Here is the build.gradle:
buildscript {
ext {
kotlinVersion = '1.2.20'
springBootVersion = '1.5.7.RELEASE'
keycloakVersion = '3.4.3.Final'
restEasyClientVersion = '3.1.4.Final'
postgresServerVersion = '10.0'
postgresJdbcDriverVersion = '42.1.4'
spekVersion = '1.1.5'
jacksonVersion = '2.8.10'
javaxWsRsVersion = '2.1'
logbackVersion = '1.2.3'
slf4jVersion = '1.7.25'
}
repositories {
mavenCentral()
jcenter()
//spring dev
maven { url 'https://repo.spring.io/snapshot' }
maven { url 'https://repo.spring.io/milestone' }
//Kotlin dev
maven { url 'http://dl.bintray.com/kotlin/kotlin-eap-1.2' }
//gradle plugins
maven { url "https://plugins.gradle.org/m2/" }
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:${kotlinVersion}")
classpath("org.jetbrains.kotlin:kotlin-allopen:${kotlinVersion}")
classpath('com.bmuschko:gradle-docker-plugin:3.2.0')
//for tests
classpath 'org.junit.platform:junit-platform-gradle-plugin:1.0.0'
}
}
allprojects {
repositories {
mavenCentral()
jcenter()
//spring dev
maven { url 'https://repo.spring.io/snapshot' }
maven { url 'https://repo.spring.io/milestone' }
}
}
subprojects {
repositories {
maven { url 'http://dl.bintray.com/kotlin/kotlin-eap-1.2' }
// for tests
maven { url "http://dl.bintray.com/jetbrains/spek" }
}
// for kotlin
apply plugin: 'kotlin'
// for tests
apply plugin: 'org.junit.platform.gradle.plugin'
apply plugin: 'kotlin-spring'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
// for tests
junitPlatform {
filters {
engines {
include 'spek'
}
}
}
sourceCompatibility = 1.9
dependencies {
// kotlin
compile("org.jetbrains.kotlin:kotlin-stdlib:${kotlinVersion}")
compile("org.jetbrains.kotlin:kotlin-test:${kotlinVersion}")
compile("org.jetbrains.kotlin:kotlin-reflect:${kotlinVersion}")
// jackson
compile "com.fasterxml.jackson.core:jackson-core:${jacksonVersion}"
compile "com.fasterxml.jackson.core:jackson-databind:${jacksonVersion}"
compile "com.fasterxml.jackson.jaxrs:jackson-jaxrs-json-provider:${jacksonVersion}"
compile "com.fasterxml.jackson.jaxrs:jackson-jaxrs-base:${jacksonVersion}"
compile "com.fasterxml.jackson.module:jackson-module-kotlin:2.9.2"
// Java WS RS
compile "javax.ws.rs:javax.ws.rs-api:${javaxWsRsVersion}"
// for tests
testCompile "org.jetbrains.spek:spek-api:${spekVersion}"
testRuntime "org.jetbrains.spek:spek-junit-platform-engine:${spekVersion}"
testCompile ("org.jetbrains.spek:spek-api:${spekVersion}") {
exclude group: 'org.jetbrains.kotlin'
}
testRuntime ("org.jetbrains.spek:spek-junit-platform-engine:${spekVersion}") {
exclude group: 'org.junit.platform'
exclude group: 'org.jetbrains.kotlin'
}
// spring security
compile('org.springframework.boot:spring-boot-starter-security')
testCompile('org.springframework.security:spring-security-test')
// begin: spring web without jackson
compile('org.springframework.boot:spring-boot-starter')
compile('org.springframework.boot:spring-boot-starter-tomcat')
compile('org.springframework:spring-web')
compile('org.springframework:spring-webmvc')
testCompile('org.springframework.boot:spring-boot-starter-test')
// end: spring web without jackson
// Keycloak
compile("org.keycloak:keycloak-spring-security-adapter:${keycloakVersion}")
compile("org.keycloak:keycloak-spring-boot-adapter:${keycloakVersion}")
compile("org.keycloak:keycloak-tomcat8-adapter:${keycloakVersion}")
compile("org.keycloak:keycloak-admin-client:${keycloakVersion}")
compile("org.jboss.resteasy:resteasy-client:${restEasyClientVersion}")
compile("org.jboss.resteasy:resteasy-jackson2-provider:${restEasyClientVersion}")
}
compileKotlin {
kotlinOptions.jvmTarget = '1.8'
kotlinOptions.allWarningsAsErrors = true
}
compileTestKotlin {
kotlinOptions.jvmTarget = '1.8'
}
}
The problem was the underlying class loader of ForkJoinPool.commonPool which is used by CompletableFuture.supplyAsync.
Because the problem and the solution is complex please refer to my other question for better understanding.
This question is only kept alive for this cross reference (and may hopefully lead others to the correct solution).
I was able to launch a Spring Boot Kotlin App from IntelliJ 2017.3. After the last IntelliJ fix update I cannot start that application from the IDE, getting this exception:
org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: #Configuration class 'AccessConfig' may not be final
I can start it from a terminal as usual: java -jar xxxx.jar
This doesn't make any sense since I am using the necessary Kotlin Spring plugin in my Gradle config:
buildscript {
ext {
kotlinVersion = '1.2.21'
springBootVersion = '2.0.0.RC1'
}
repositories {
mavenCentral()
maven { url "https://repo.spring.io/snapshot" }
maven { url "https://repo.spring.io/milestone" }
jcenter()
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:${kotlinVersion}")
classpath("org.jetbrains.kotlin:kotlin-allopen:${kotlinVersion}")
classpath 'org.asciidoctor:asciidoctor-gradle-plugin:1.5.3'
classpath 'org.junit.platform:junit-platform-gradle-plugin:1.0.2'
classpath "org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:2.5"
}
}
apply plugin: 'kotlin'
apply plugin: 'kotlin-spring'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'maven'
...
sourceCompatibility = 1.8
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
repositories {
mavenLocal()
maven { url "https://repo.spring.io/snapshot" }
maven { url "https://repo.spring.io/milestone" }
maven { url "http://repo.maven.apache.org/maven2" }
maven { url 'https://jitpack.io' }
}
ext {
springCloudVersion = 'Finchley.M5'
mmaReleaseTrainVersion = 'Callao-SNAPSHOT'
junitVersion = '5.0.2'
}
Any ideas?
UPDATE:
A simpler way to reproduce, just create a Spring Boot project with the Spring initializer from IntelliJ, you'll see the same result:
DemoApplication:
#SpringBootApplication
class DemoApplication
fun main(args: Array<String>) {
SpringApplication.run(DemoApplication::class.java, *args)
}
Error:
org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: #Configuration class 'DemoApplication' may not be final. Remove the final modifier to continue.
Offending resource: com.example.demo.DemoApplication
build.gradle:
buildscript {
ext {
kotlinVersion = '1.2.10'
springBootVersion = '1.5.10.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:${kotlinVersion}")
classpath("org.jetbrains.kotlin:kotlin-allopen:${kotlinVersion}")
}
}
apply plugin: 'kotlin'
apply plugin: 'kotlin-spring'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
repositories {
mavenCentral()
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-web')
compile("org.jetbrains.kotlin:kotlin-stdlib-jre8:${kotlinVersion}")
compile("org.jetbrains.kotlin:kotlin-reflect:${kotlinVersion}")
testCompile('org.springframework.boot:spring-boot-starter-test')
}
First of all, it's all due to class Kotlin class definition:
The open annotation on a class is the opposite of Java's final: it allows others to inherit from this class. By default, all classes in Kotlin are final
so if you are free to modify your source code, you can make your class not final, just adding open to it's signature as follows:
#SpringBootApplication
open class DemoApplication
fun main(args: Array<String>) {
SpringApplication.run(DemoApplication::class.java, *args)
}
or one of the possible solutions, according to this article:
The #SpringBootApplication is a convenience annotation that marks the class with the #Configuration, #EnableAutoConfiguration and #ComponentScan annotations. It is the #Configuration annotation that forces the use of the open keyword.
Is to try to remove the #SpringBootApplication annotation and annotate your class with #EnableAutoConfiguration and #ComponentScan to solve this issue.
Fixed upgrading IntelliJ's Kotlin pluging to 1.2.21: https://plugins.jetbrains.com/plugin/6954-kotlin/update/42501
If you get this error on IntelliJ, even with kotlin-spring plugin, you may want to check if the annotation processing has been enabled in IntelliJ.
If plugin update does not help, check if version of kotlin in gradle matches ide's kotlin version.
This solved it for me (just add the following line to plugins):
plugins {
id "org.jetbrains.kotlin.plugin.spring" version "1.5.10"
}
Or this approach:
buildscript {
dependencies {
classpath "org.jetbrains.kotlin:kotlin-allopen:$kotlin_version"
}
}
apply plugin: "kotlin-spring"
Or in Maven:
<compilerPlugins>
<plugin>spring</plugin>
</compilerPlugins>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-allopen</artifactId>
<version>${kotlin.version}</version>
</dependency>
For not Spring Applications do the following:
plugins {
id "org.jetbrains.kotlin.plugin.allopen" version "1.5.10"
}
or this approach:
buildscript {
dependencies {
classpath "org.jetbrains.kotlin:kotlin-allopen:$kotlin_version"
}
}
apply plugin: "kotlin-allopen"
After that specify the list of annotations that will make classes open
allOpen {
annotation("com.my.Annotation")
}
Or work with meta-annotations:
#com.my.Annotation
annotation class MyFrameworkAnnotation
#MyFrameworkAnnotation
class MyClass //all-open
how to use all-open with Maven:
<plugin>
<artifactId>kotlin-maven-plugin</artifactId>
<groupId>org.jetbrains.kotlin</groupId>
<version>${kotlin.version}</version>
<configuration>
<compilerPlugins>
<plugin>all-open</plugin>
</compilerPlugins>
<pluginOptions>
<option>all-open:annotation=com.my.Annotation</option>
<option>all-open:annotation=com.their.AnotherAnnotation</option>
</pluginOptions>
</configuration>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-allopen</artifactId>
<version>${kotlin.version}</version>
</dependency>
</dependencies>
If you need to have open behavior default to all Spring related classes in kotlin, you can use
kotlin-allopen plugin from here
https://search.maven.org/classic/#search%7Cga%7C1%7Ckotlin%20allopen
More info :
https://www.baeldung.com/kotlin-allopen-spring