EnableEurekaClient import doesn't exist - spring-boot

I added spring-cloud-starter-netflix-eureka-client gradle depedency in
my project and shrik the depedency. But when go use
#EnableEurekaClient in my Main class it show me suggestion create
#EnableEurekaClient annotation. Don't show any import file of eureka
client.
Unresolved reference: EnableEurekaClient
productserviceApplication.kt
package com.main.productservice
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
#SpringBootApplication
#EnableEurekaClient
class ProductServiceApplication
fun main(args: Array<String>) {
runApplication<ProductServiceApplication>(*args)
}
gradle.kt
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
id("org.springframework.boot") version "2.5.2"
id("io.spring.dependency-management") version "1.0.11.RELEASE"
kotlin("jvm") version "1.5.20"
kotlin("plugin.spring") version "1.5.20"
}
group = "com.main"
version = "0.0.1-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_11
repositories {
mavenCentral()
}
dependencies {
implementation("org.springframework.boot:spring-boot-starter-data-mongodb")
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
implementation("org.jetbrains.
kotlin:kotlin-reflect")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
implementation("org.springframework.cloud:spring-cloud-starter-netflix-eureka-client")
testImplementation("org.springframework.boot:spring-boot-starter-test")
}
tasks.withType<KotlinCompile> {
kotlinOptions {
freeCompilerArgs = listOf("-Xjsr305=strict")
jvmTarget = "11"
}
}
tasks.withType<Test> {
useJUnitPlatform()
}
I am getting error at #EnableEurekaClient

#EnableEurekaClient is deprecated no need to annotate at springboot main applicationit is fine if we add the spring-cloud-starter-netflix-eureka-client dependency in pom and if we have the application name in yml or properties file it will be registered to Eureka Server

Make sure to import the Spring Cloud BOM in your Gradle build definition:
extra["springCloudVersion"] = "2020.0.3"
dependencyManagement {
imports {
mavenBom("org.springframework.cloud:spring-cloud-dependencies:${property("springCloudVersion")}")
}
}
The EnableEurekaClient annotation is in package org.springframework.cloud.netflix.eureka. You should add the following import to your main application file:
import org.springframework.cloud.netflix.eureka.EnableEurekaClient

Related

How to enable spring security kotlin DSL?

How can we enable support for the spring security kotlin DSL?
As you can see from the Screenshot of the IDE (IntelliJ), the DSL is not available:
This is the full SecurityConfig.kt file:
package com.example.backend.core
import org.springframework.context.annotation.Bean
import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity
import org.springframework.security.config.web.server.ServerHttpSecurity
import org.springframework.security.web.server.SecurityWebFilterChain
#EnableWebFluxSecurity
class SecurityConfig {
#Bean
fun springSecurityFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain? {
return http {
csrf { disable() }
formLogin { disable() }
httpBasic { disable() }
// ...
}
}
}
This is our build.gradle.kts
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
val springBootVersion = "2.4.2"
plugins {
id("org.springframework.boot") version "2.4.2"
id("io.spring.dependency-management") version "1.0.11.RELEASE"
kotlin("jvm") version "1.4.21"
kotlin("plugin.spring") version "1.4.21"
}
group = "com.example"
version = "0.0.1-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_11
configurations {
compileOnly {
extendsFrom(configurations.annotationProcessor.get())
}
}
repositories {
mavenCentral()
}
dependencies {
implementation("org.springframework.boot:spring-boot-starter-actuator")
implementation("org.springframework.boot:spring-boot-starter-oauth2-resource-server")
implementation("org.springframework.boot:spring-boot-starter-oauth2-client")
implementation("org.springframework.boot:spring-boot-starter-security")
implementation("org.springframework.boot:spring-boot-starter-webflux")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
implementation("io.projectreactor.kotlin:reactor-kotlin-extensions")
implementation("org.flywaydb:flyway-core")
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-reactor")
developmentOnly("org.springframework.boot:spring-boot-devtools")
runtimeOnly("io.micrometer:micrometer-registry-prometheus")
runtimeOnly("org.postgresql:postgresql")
annotationProcessor("org.springframework.boot:spring-boot-configuration-processor")
testImplementation("org.springframework.boot:spring-boot-starter-test")
testImplementation("io.projectreactor:reactor-test")
testImplementation("org.springframework.security:spring-security-test")
}
tasks.withType<KotlinCompile> {
kotlinOptions {
freeCompilerArgs = listOf("-Xjsr305=strict")
jvmTarget = "11"
}
}
tasks.withType<Test> {
useJUnitPlatform()
}
And this is the intellij version:
IntelliJ IDEA 2020.3 (Community Edition)
Build #IC-203.5981.155, built on November 30, 2020
Runtime version: 11.0.9+11-b1145.21 amd64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o.
Linux 5.4.0-64-generic
GC: ParNew, ConcurrentMarkSweep
Memory: 1979M
Cores: 12
Registry: compiler.automake.allow.when.app.running=true
Non-Bundled Plugins: Key Promoter X, Dart, io.flutter, Lombook Plugin, org.jetbrains.kotlin
Current Desktop: X-Cinnamon
Do we miss some dependency? Does it require any specific intellij setup?
You need to manually import the ServerHttpSecurity invoke.
import org.springframework.security.config.web.server.invoke
Because of this Kotlin issue in 1.4, the IDE does not suggest it to you as it should.
This is scheduled to be fixed in Kotlin 1.4.30.
I noticed your question was regarding WebFlux, but just complementing #Eleftheria's answer.
For WebMvc folks you should import:
import org.springframework.security.config.web.servlet.invoke
(servlet vs server)
What worked for me was to explicitly call the .invoke method on the HttpSecurity object:
http.invoke {
csrf { disable }
}
which will force my IDE (I'm using IntelliJ) to import the .invoke.
This DSL is built in starting since Spring Security 5.3 version. For example the org.springframework.security:spring-security-config:5.3.4.RELEASE library has it: org.springframework.security.config.web.servlet.HttpSecurityDsl#csrf And for example the
compile group: 'org.springframework.boot', name: 'spring-boot-starter-security', version: '2.3.3.RELEASE'
library will contain it.

Dockerfile can not build jar file : Main class name has not been configured

I am building a dockerfile for a microservices architecture (spring) but I find some difficulties.
I got this error when buidling the boot jar
Main class name has not been configured and it could not be resolved
my dockerfile is
FROM gradle:6.5-jdk11 AS TEMP_BUILD_IMAGE
ENV APP_HOME=/usr/app/
WORKDIR $APP_HOME
COPY build.gradle.kts settings.gradle.kts $APP_HOME
COPY gradle $APP_HOME/gradle
COPY --chown=gradle:gradle . /home/gradle/src
USER root
RUN chown -R gradle /home/gradle/src
RUN gradle bootJar || return 0
COPY . .
RUN gradle clean bootJar
FROM openjdk:11-jdk
ENV ARTIFACT_NAME=app.jar
ENV APP_HOME=/usr/app/
WORKDIR $APP_HOME
COPY --from=TEMP_BUILD_IMAGE $APP_HOME/build/libs/$ARTIFACT_NAME .
ENTRYPOINT exec java -jar ${ARTIFACT_NAME}
The error is in 9th step
RUN gradle bootJar || return 0
I tried to put the name of the class in the gradle file, and i got this error
Exception in thread "main" java.lang.NoSuchMethodException: com.test.config.ConfigServerApp.main([Ljava.lang.String;)
this is my gradle file
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import org.springframework.boot.gradle.tasks.bundling.BootJar
plugins {
id("io.spring.dependency-management") version "1.0.9.RELEASE"
id("org.springframework.boot") version "2.3.0.RELEASE"
kotlin("plugin.spring") version "1.3.72"
kotlin("jvm") version "1.3.72"
}
repositories {
mavenCentral()
}
group = "com.test"
version = "1.0.0"
java.sourceCompatibility = JavaVersion.VERSION_11
extra["springCloudVersion"] = "Hoxton.SR5"
dependencies {
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
implementation("org.springframework.cloud:spring-cloud-config-server")
implementation("org.springframework.cloud:spring-cloud-config-monitor")
implementation("org.springframework.cloud:spring-cloud-starter-bus-amqp")
testImplementation("org.springframework.boot:spring-boot-starter-test") {
exclude(group = "org.junit.vintage", module = "junit-vintage-engine")
}
}
dependencyManagement {
imports {
mavenBom("org.springframework.cloud:spring-cloud-dependencies:${property("springCloudVersion")}")
}
}
tasks.withType<Test> {
useJUnitPlatform()
}
tasks.withType<KotlinCompile> {
kotlinOptions {
jvmTarget = "1.8"
freeCompilerArgs = listOf("-Xjsr305=strict")
}
}
tasks.withType<BootJar> {
archiveFileName.set("app.jar")
mainClassName = "com.test.config.ConfigServerApp"
}
this is my main class (i m using kotlin)
package com.test.config
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import org.springframework.cloud.config.server.EnableConfigServer
#EnableConfigServer
#SpringBootApplication
class ConfigServerApp
fun main(args : Array<String>)
{
runApplication<ConfigServerApp>(*args)
}
when I launch the application from my editor it works, but when I launch it from the jar file or from docker, it does not work
how can i fix this please?
Just to make a formal answer from my comment since it worked. As I explained in the comment: When we use Kotlin, the real main class is generated behind the scenes with Kt suffix, you should find it in your build folder after running gradle bootJar.
You should update the mainClassName to com.test.config.ConfigServerAppKt as shown below:
tasks.withType<BootJar> {
archiveFileName.set("app.jar")
mainClassName = "com.test.config.ConfigServerAppKt"
}
As explained in the Kotlin docs :
All the functions and properties declared in a file app.kt inside a package org.example, including extension functions, are compiled into static methods of a Java class named org.example.AppKt.
So your main method will be part of java class named ConfigServerAppKt after compilation. So you should change your mainClassName to com.test.config.ConfigServerAppKt.

How to import amazon s3 for spring boot using gradle kotlin dsl

I have tried to import s3 in my spring boot project following the instructions on the official docs but the dependencies aren't being resolved. https://docs.aws.amazon.com/sdk-for-java/v2/developer-guide/setup-project-gradle.html
I updated the software.amazon.awssdk:bom from the documentation to the latest 2.7.16 hoping this will fix it.
this is my build.gradle.kts file
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
kotlin("plugin.jpa") version "1.3.31"
id("org.springframework.boot") version "2.1.5.RELEASE"
id("io.spring.dependency-management") version "1.0.7.RELEASE"
kotlin("jvm") version "1.3.31"
kotlin("plugin.spring") version "1.3.31"
kotlin("plugin.noarg") version "1.3.31"
}
allOpen {
annotation("javax.persistence.Entity")
annotation("javax.persistence.MappedSuperclass")
annotation("javax.persistence.Embeddable")
}
noArg {
annotation("javax.persistence.Entity")
}
group = "com.leaveentry"
version = "0.0.1-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_1_8
repositories {
mavenCentral()
}
dependencies {
implementation("org.springframework.boot:spring-boot-starter-data-jpa")
implementation("org.springframework.boot:spring-boot-starter-security")
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
implementation("com.google.api-client:google-api-client:1.25.0")
implementation("io.jsonwebtoken:jjwt-api:0.10.5")
implementation(platform("software.amazon.awssdk:bom:2.7.16"))
implementation("software.amazon.awssdk:s3")
runtimeOnly("com.h2database:h2")
runtimeOnly("mysql:mysql-connector-java")
runtimeOnly("io.jsonwebtoken:jjwt-impl:0.10.5")
runtimeOnly("io.jsonwebtoken:jjwt-jackson:0.10.5")
annotationProcessor("org.springframework.boot:spring-boot-configuration-processor")
testImplementation("org.springframework.boot:spring-boot-starter-test")
testImplementation("org.springframework.security:spring-security-test")
}
tasks.withType<KotlinCompile> {
kotlinOptions {
freeCompilerArgs = listOf("-Xjsr305=strict")
jvmTarget = "1.8"
}
}
When I try to import s3 related classes, they are not resolved.
import com.amazonaws.auth.AWSStaticCredentialsProvider
import com.amazonaws.auth.BasicAWSCredentials
import com.amazonaws.regions.Regions
import com.amazonaws.services.s3.AmazonS3ClientBuilder
import com.amazonaws.services.s3.model.CannedAccessControlList
import com.amazonaws.services.s3.model.PutObjectRequest
I've had similar issues and although I didn't manage to make that specific version of amazon bom work, I found a workaround.
Add to your build.gradle.kts the following lines:
dependencyManagement {
imports {
mavenBom("com.amazonaws:aws-java-sdk-bom:1.11.228")
}
}
and the following implementation inside the dependencies block:
implementation("software.amazon.awssdk:s3:2.10.1")
Note: Alternatively, you can add the following if you want to include all the services at once:
implementation("software.amazon.awssdk:aws-sdk-java:2.10.1")

Kotlin: Cannot inline bytecode built with JVM target 1.8 into byte code with JVM target 1.6

I have an issue when trying to add a few dependencies to my kotlin spring project. I used the spring boot initializer to get a basic project running.
My problem: If I uncomment jackson or either Koin dependency then my build fails with the mentioned in the titile
Here is the build.gradle.kts file :
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
kotlin("plugin.jpa") version "1.3.31"
id("org.springframework.boot") version "2.2.0.M4"
id("io.spring.dependency-management") version "1.0.7.RELEASE"
kotlin("jvm") version "1.3.31"
kotlin("plugin.spring") version "1.3.31"
}
group = "com.example"
version = "0.0.1-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_1_8
val developmentOnly by configurations.creating
configurations {
runtimeClasspath {
extendsFrom(developmentOnly)
}
}
repositories {
mavenCentral()
maven { url = uri("https://repo.spring.io/snapshot") }
maven { url = uri("https://repo.spring.io/milestone") }
}
dependencies {
implementation("org.springframework.boot:spring-boot-starter-data-jpa")
implementation("org.springframework.boot:spring-boot-starter-thymeleaf")
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
developmentOnly("org.springframework.boot:spring-boot-devtools")
runtimeOnly("com.h2database:h2")
testImplementation("org.springframework.boot:spring-boot-starter-test") {
exclude(group = "org.junit.vintage", module = "junit-vintage-engine")
exclude(group = "junit", module = "junit")
}
// jackson
//implementation("com.fasterxml.jackson.module:jackson-module-kotlin:2.9.+")
// Koin
//implementation("org.koin:koin-core:2.0.1")
// Koin Test
//implementation("org.koin:koin-test:2.0.1")
}
tasks.withType<Test> {
useJUnitPlatform()
}
tasks.withType<KotlinCompile> {
kotlinOptions {
freeCompilerArgs = listOf("-Xjsr305=strict")
jvmTarget = "1.8"
}
}
I Have already tried all the solutions from this previous question someone else asked: Cannot inline bytecode built with JVM target 1.8 into bytecode that is being built with JVM target 1.6
I already had the KotlinCompile jvmTarget option in there and it has been set in my build settings as well. Any help is appreciated thanks!
A lot of the answers to this are android specific - using kotlin to write a spring boot app isn't that well documented or supported but still awesome. This setting in intelij cleared this error for me - hope it helps someone, my kotlin compiler had a target set to 1.6:

Spring complains #EnableWebMvc when I haven't used that annotation nor any dependency related to MVC

I am working on a reactive spring boot api server. I first wanted to use MVC pattern, but I thought reactor would be a good idea.
So I have deleted all spring dependencies on MVC (I believe).
But spring keeps complaining that I can't use #EnableWebMvc along #EnableWebFlux.
Following is my error log
Caused by: java.lang.IllegalStateException: The Java/XML config for Spring MVC and Spring WebFlux cannot both be enabled, e.g. via #EnableWebMvc and #EnableWebFlux, in the same application.
What possibly could be the problem? I sure did updated my dependencies.
And following is my build.gradle.kts file
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
buildscript {
repositories {
mavenCentral()
}
dependencies {
"classpath"(group = "gradle.plugin.com.palantir.gradle.docker", name = "gradle-docker")
}
}
plugins {
kotlin("plugin.jpa") version "1.3.40"
id("org.springframework.boot") version "2.1.6.RELEASE"
id("io.spring.dependency-management") version "1.0.7.RELEASE"
id("com.palantir.docker") version "0.22.1"
kotlin("jvm") version "1.3.40"
kotlin("plugin.spring") version "1.3.40"
}
group = "com.mycompany"
version = "0.0.1-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_1_8
val developmentOnly by configurations.creating
configurations {
runtimeClasspath {
extendsFrom(developmentOnly)
}
}
repositories {
mavenCentral()
maven(url = "https://repo.spring.io/snapshot")
maven(url = "https://repo.spring.io/milestone")
}
extra["springCloudVersion"] = "Greenwich.SR1"
dependencies {
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("org.springframework.boot:spring-boot-starter-batch")
implementation("org.springframework.boot:spring-boot-starter-oauth2-client")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
//reactor
implementation("io.projectreactor:reactor-core")
implementation("org.springframework.boot:spring-boot-starter-webflux")
implementation("org.springframework.data:spring-data-jdbc:1.0.0.r2dbc-SNAPSHOT")
implementation("org.springframework.data:spring-data-r2dbc:1.0.0.M1")
implementation("io.r2dbc:r2dbc-spi:1.0.0.M5")
implementation("io.r2dbc:r2dbc-postgresql:1.0.0.M6")
developmentOnly("org.springframework.boot:spring-boot-devtools")
runtimeOnly("org.postgresql:postgresql")
testImplementation("org.springframework.boot:spring-boot-starter-test")
testImplementation("io.projectreactor:reactor-test")
testImplementation("org.springframework.batch:spring-batch-test")
annotationProcessor("org.springframework.boot:spring-boot-configuration-processor")
testImplementation("org.springframework.boot:spring-boot-starter-test") {
exclude(group = "org.junit.vintage", module = "junit-vintage-engine")
exclude(group = "junit", module = "junit")
}
}
dependencyManagement {
imports {
mavenBom("org.springframework.cloud:spring-cloud-dependencies:${property("springCloudVersion")}")
mavenBom("io.projectreactor:reactor-bom:Bismuth-RELEASE")
}
}
tasks.withType<KotlinCompile> {
kotlinOptions {
freeCompilerArgs = listOf("-Xjsr305=strict")
jvmTarget = "1.8"
}
}
task<Copy>("unpack") {
dependsOn(tasks.getByName("bootJar"))
from(zipTree(tasks.getByName("bootJar").outputs.files.singleFile))
into("build/dependency")
}
I figured it out by deleting each dependencies I have.
The problem was
implementation("org.springframework.boot:spring-boot-starter-web")
After deleting this, It worked. Maybe above dependency has it's own dependencies on WebMvc but I'm not sure.

Resources