Fantastic issues and how to solve them: Spring Boot + Gradle upgrade addition.
We’ve all been there: tasks that need to be done but that no one wants to tackle. Upgrading the Spring framework is often one of those tasks. It’s like a chore that looms over your project, especially when you’re moving from an older version (in my case, 2.3.12) to a newer one (2.7.12).
Why is this upgrade so intimidating? Well, in the world of coding, plans don’t always unfold as expected. You might start with a clear roadmap, but soon find yourself facing unexpected challenges and obstacles.
So, let’s dive into the process of refreshing your Spring framework. We’ll explore the changes that need to be made, the challenges that might arise, and the solutions that can help you navigate this tricky upgrade. Whether you’re a seasoned developer or just getting started with Spring, this guide will provide practical insights and examples to make the transition as smooth as possible.
Disclaimer
The solutions provided here are a blend of insights gathered from various sources, including Stack Overflow, ChatGPT, official documentations, and more. My goal is to help you find these solutions faster and with less hassle.
Please note that this guide is not a comprehensive upgrade tutorial. Instead, it focuses on addressing specific issues that I’ve encountered during the upgrade process, which were particularly troublesome. Always remember to update your dependencies according to the new versions and usage, as this guide won’t cover every detail of the upgrade process.
Spring Boot Version 🍃
In the build Gradle file you locate “springBootVersion” and change it to the version you want, in my case 2.7.12.
springBootVersion = '2.7.12'
And that’s it! enjoy! Please like and sub.. wait.. there is more??
Before we jumped into the issues I’ve faced, I will also note that I’m using Gradle wrapper and upgrade it from “6.6.1” to “8.1.1”.
Maven ☕
So, a small thing that drove me crazy at start was the Maven plug in, they’ve changed it from maven to:
plugins {
id 'maven-publish'
}
Although it’s a small change just notifying so you won’t waste time on it.
Compile to Implementation 🥂
One more small change that is needed when doing this type of upgrade is the change in the dependencies, from:
compile (group:'...', name:'...', version:'...')
To:
implementation group:'...', name:'...', version:'...'
Eureka 🔭
The issue I’ve encountered came from an know please, the error was not clear and need to break my head around it:
[DiscoveryClient-CacheRefreshExecutor-0] [] [] WARN c.n.d.s.t.d.RetryableEurekaHttpClient — Request execution failed with message: I/O error on GET request for “/eureka/apps/": Connect to localhost failed: Connection refused (Connection refused); nested exception is org.apache.http.conn.HttpHostConnectException: Connect to localhost failed: Connection refused (Connection refused)
Basically, I had a hard time connecting to the discovery service.
The solution I’ve found to make my service connect the eureka service is to add few lines in the application.properties file:
eureka.client.register-with-eureka=true
eureka.client.fetch-registry=true
eureka.client.fetchRegistry=true
eureka.zone.url=${your.eureka.url}
eureka.client.serviceUrl.defaultZone=${your.eureka.url}
Spring Admin 👨💼
Another issue I’ve faced is with the Spring Admin service, after the upgrade my service was not establishing a connection with the Spring Admin, this was due to port issues.
My service didn’t exposed the ports that I wanted it to expose for the “actuator” and for that reason Spring admin kept throwing me connection issues.
The solution to this, was to add the following to the application.properties:
mgt.port=${port.for.spring.admin}
management.server.port=${port.for.spring.admin}
management.endpoints.web.base-path=/actuator
management.endpoints.web.exposure.include=*
health.url=http://localhost:${port.for.spring.admin}/actuator/health
(just replace ${port.for.spring.admin} with the port that the Spring admin service is trying to connect to)
Power Mock 🐒
Another issue was with the tests, after the upgrade the tests kept on failing with several error.
Invoking the beforeTestMethod method on PowerMock test listener org.powermock.api.extension.listener.AnnotationEnabler@bb21063 failed.
To combat that error I’ve first needed to align my dependencies:
testImplementation "org.powermock:powermock-module-junit4:1.7.0RC4"
testImplementation "org.powermock:powermock-api-mockito2:1.7.0RC4"
testImplementation "org.mockito:mockito-core:2.8.47"
Then needed to add to the tests the following:
@PowerMockIgnore("jdk.internal.reflect.*")
Note: this is only for Spring + Gradle upgrade (with Java 8), if you are using/upgrading to Java 11+ should use the following:
testImplementation 'junit:junit:4.13.2' // Updated version
testImplementation 'org.easymock:easymock:4.3' // Updated version
testImplementation 'io.netty:netty-codec-http:4.1.68.Final' // Updated version
testImplementation "org.powermock:powermock-module-junit4:2.0.9" // Updated version
testImplementation "org.powermock:powermock-api-mockito2:2.0.9" // Updated version
testImplementation "org.mockito:mockito-core:3.12.4" // Updated version
Spring Core ☃
One more issue that can pop is after the upgrade, it’s the following:
org.springframework.util.ObjectUtils.nullSafeConciseToString(Ljava/lang/Object;)Ljava/lang/String; java.lang.NoSuchMethodError: org.springframework.util.ObjectUtils.nullSafeConciseToString(Ljava/lang/Object;)Ljava/lang/String;
This usually happens to custom exceptions that are implemented on your local project, you’ll get this error with 500 and not what you’d expect, for example 404 not found.
To solve this problem need to update “spring-core”, in my case I’ve changed it from “5.3.20” to “5.3.27”.
Summarization
As mentioned earlier, this guide isn’t intended to be a full-on tutorial. Rather, it’s a collection of valuable insights and “gems” I’ve discovered during the rollercoaster journey of upgrading Spring Boot and Gradle. It’s a small glimpse into the challenges and triumphs of the process, and I hope that those who find themselves on a similar path will find these insights useful. Happy upgrading!