Why am I getting this NoClassDefFound error? - maven

I am trying to create a contract in Spring(https://spring.io/projects/spring-cloud-contract)
This is the error I keep getting:
Tests in error: ContractVerifierTest.validate_shouldReturnPreviousAddress:19 » NoClassDefFound
And this is the line causing the problem:
// given:
MockMvcRequestSpecification request = given();
This is my groovy file:
package contracts
import org.springframework.cloud.contract.spec.Contract
Contract.make {
request {
method 'GET'
url value(consumer('/echo'), producer('/echo'))
}
response {
status 200
headers {
header(
'Content-Type', value(consumer('text/plain;charset=ISO-8859-1'), producer(regex('text/plain;charset=ISO-8859-1')))
)
}
body(
"Send me something!"
)
}
priority 1
}
And I have included the following in my pom file:
<build>
<plugins>
<plugin>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-contract-maven-plugin</artifactId>
<version>1.2.2.RELEASE</version>
<extensions>true</extensions>
<configuration>
<baseClassForTests>[baseClass link]</baseClassForTests>
</configuration>
</plugin>
</plugins>
</build>
As far as I can see, the line causing this problem is correct. Is there something else I could be missing?
EDIT:
On closer inspection, this seems to be the real cause of the issue:
java.lang.NoClassDefFoundError: io/restassured/internal/common/assertion/AssertParameter

java.lang.NoClassDefFoundError
The above error usually happens due to a mismatch between Spring Cloud and Spring Boot versions.
Spring Cloud releases are tied to specific Spring Boot versions. One can refer the following useful table in the Spring Cloud documentation:
Release train Spring Boot compatibility
Release Train
Boot Version
2021.0.x aka Jubilee
2.6.x, 2.7.x (Starting with 2021.0.3)
2020.0.x aka Ilford
2.4.x, 2.5.x (Starting with 2020.0.3)
Hoxton
2.2.x, 2.3.x (Starting with SR5)
Greenwich
2.1.x
Finchley
2.0.x
Edgware
1.5.x
Dalston
1.5.x
Note: It is recommended to use rest-assured that comes out of the box with Spring, to avoid any version mismatches.

Related

Swagger UI - org.springdoc - throws whitelabel error

To mitigate Log4j vulnerability(2.14 to 2.17), we have migrated from spring boot 2.4.0 to 2.6.4. Since Spring Fox was having some issues we migrated to spring doc.
Swagger UI works fine on local, when moved to server (JBOSS), we get white label error , sometimes it works .
can someone help me on this or best alternative to mitigate the Log4j vulnerability.
Used Below Dependency in pom.xml:
Spring Boot version from 2.4.0 to 2.6.4
<dependency>
<groupId>org.springdoc</groupId>
<artifact>springdoc-openapi-ui</artifact>
<version>1.6.6</version>
</dependency>
application.properties :
application-description=#project.description#
application-version=#project.version#
main class :
#Bean
public OpenAPI Internal_APIs(#Value("${application-description}") String appDesciption, #Value("${application-version}") String appVersion)
{
return new OpenAPI()
.info(new Info()
.title("Internal")
.version(appVersion)
.description(appDesciption)
.license(new License()
.name("Apache 2.0")
.url("http://springdoc.org")))
.externalDocs(new External Documentation()
.url("http://springdoc.org"));
}

Spring cloud Contract verifier baseClassMapping no longer working in 3.0.0

The below config works fine perfectly in spring-cloud-starter-contract-verifier 2.2.4.RELEASE. But with 3.0.0 it throws the error below, it is not deprecated in the docs. Does anyone know if the behavior is changed ?
.pom file
<plugin>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-contract-maven-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<baseClassForTests>com.mycompany.selectservice.integrationtest.</baseClassForTests>
<baseClassMappings>
<baseClassMapping>
<contractPackageRegex>.*select.*</contractPackageRegex>
<baseClassFQN>com.mycompany.selectservice.integrationtest.SelectBaseTest</baseClassFQN>
</baseClassMapping>
</baseClassMappings>
</configuration>
</plugin>
SelectTest.validate_contract_0:33 » IllegalState You haven't configured a MockMvc.
My spring boot version is 2.2.4.RELEASE, So it means that spring-cloud-starter-contract-verifier 3.0.0 cannot be used with spring boot 2.2.4.release?
As described in our project page (https://spring.io/projects/spring-cloud) you have to use Boot 2.4 to use Spring Cloud Contract. Also, that functionality does work - most likely you're using some old imports or you're using JUnit4 (the default now is JUnit5).

Upgrading To Spring Boot 2.4.0 Breaks Legacy Applications

I'm upgrading a set of legacy applications to Spring Boot 2.4.0, running on OpenJDK 8, deployed to Pivotal Cloud Foundry.
The app and all its unit tests ran perfectly under Spring 2.3.4-RELEASE before I started this upgrade.
There was work to be done right off the bat: Spring Boot 2.4.0 brings in JUnit 5.x, so I had to fix all the JUnit 4.x tests to use the new annotations and classes.
After fixing all the tests I tried to run in IntelliJ 2020.2. All the tests failed, for the same reason: java.lang.IllegalStateException: Failed to load ApplicationContext. The root of the stack trace gives this cause:
Caused by: java.lang.ClassNotFoundException: org.springframework.boot.context.properties.ConfigurationBeanFactoryMetadata
They removed a fundamental class from Spring Boot for the sake of Kubernetes, from the sound of it. I'm not sure I see why.
I did a Google search and found this explanation and fix.
I added spring.config.use-legacy-processing to my application.yml and a test application.properties file:
spring:
config:
use-legacy-processing: true
Still no joy - all the tests fail.
I'm confused by other links I've seen. Is this an issue with spring-cloud-dependencies? I'm reading that there might an issue with the Hoxton version. Do I need to add it to my app? It never needed this dependency before the upgrade:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR9</version>
<type>pom</type>
<scope>import</scope>
</dependency>
The problem is bigger than the tests. If I ignore them and try to run the app I still fail:
ERROR [main]: Application run failed |ApplicationName=Risk_Advisor | sourcedfrom=ERROR
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'configurationPropertiesBeans' defined in class path resource [org/springframework/cloud/autoconfigure/ConfigurationPropertiesRebinderAutoConfiguration.class]: Post-processing of merged bean definition failed; nested exception is java.lang.IllegalStateException: Failed to introspect Class [org.springframework.cloud.context.properties.ConfigurationPropertiesBeans] from ClassLoader [sun.misc.Launcher$AppClassLoader#18b4aac2]
The root cause is the same:
java.lang.NoClassDefFoundError: org/springframework/boot/context/properties/ConfigurationBeanFactoryMetadata
when ever you do upgrades on spring, pls always look at the spring / cloud compatibility matrix and then upgrades the versions for both spring and cloud accordingly:
Also, pls note that you may not have added spring cloud as direct dependency but it may be coming into your final jar as a transitive dependency so its always better to add below spring cloud dependency as direct dependency management in your pom to stay away from any conflicts:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring.cloud-version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
I got the same error when migrating to Spring Boot 2.4.4 which forced me to migrate to Spring Cloud 2020.0.1 as well. In my case, the error was due to Bootstrap, provided by spring-cloud-commons, not being any longer enabled by default. I had to re-enabled it by adding the following new starter in my build.gradle file as stated here:
implementation 'org.springframework.cloud:spring-cloud-starter-bootstrap'
You say your app doesn't use Spring Cloud, so I'm not sure why you ended up facing which seems to be the same issue. Hopefully this workaround might work for you and help out others using Spring Cloud.
To fix the failing JUnit 4 tests, I have just added it as a dependency:
testImplementation 'junit:junit:4.13'
I had the same error for JUnit 4 tests after migration my project to Spring Boot 2.5.6. Adding JUnit 4 dependencies did not fix this error as well as migration JUnit 4 tests to JUnit 5. My project does not define Spring Cloud dependencies in build.gradle so in order to find where those dependencies come from I simply ran gradle command:
gradle dependencyInsight --dependency org.springframework.cloud
and saw that spring cloud dependencies were pulled within one of the custom libraries. Upgrade spring cloud dependencies to a compatible version there and reimport custom library fixed the error.

Moving from Spring Boot 2.0.0.M6 to 2.0.0.M7 - dependencies.dependency.version' for org.springframework.social:spring-social-security:jar is missing

I'm trying to upgrade my application from Spring Boot 2.0.0.M6 to 2.0.0.M7
After upgrading my Maven application I ran into the following errors:
For example the following dependency
<dependency>
<groupId>org.springframework.social</groupId>
<artifactId>spring-social-security</artifactId>
</dependency>
shows the following error:
Project build error: 'dependencies.dependency.version' for org.springframework.social:spring-social-security:jar is missing.
What am I doing wrong and how to fix it?
Dependency management and auto-configuration for Spring Social was removed from Spring Boot in 2.0 M7. The longer-term plan is for it to move into Spring Social itself but that has yet to happen.

"mvn spring-boot:run": "/health" endpoint with project version

I'd like to use Spring Boot's actuator endpoint /info to show project metadata such as the Maven-provided project version.
To do so, I followed the appropriate part in Spring Boot's documentation.
The shown solution works for me when my Spring Boot application starts up with java -jar [...].
Just as the documentation says, it doesn't work when the application starts up with mvn spring-boot:run — in that case, my /info endpoint reports "version":"#project.version#".
The documentation mentions that one has to properly configure Spring Boot's Maven plugin, but I could not find any information about doing so.
How can I configure my Maven project in order to have a mvn spring-boot:run-started Spring Boot application to show project metadata?
Configure the spring-boot-maven-plugin with <addResources>false</addResources>.
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.3.0.BUILD-SNAPSHOT</version>
<configuration>
<addResources>false</addResources>
</configuration>
</plugin>
Source

Resources