ValidationException with Spring boot and Jetty - spring-boot

I set up a spring boot application(1.4.0.RELEASE) with the following configuration
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
As expected, this ran with embedded Tomcat. I then thought of trying the same with a Jetty server and followed the steps mentioned in the documentation here:
Using Jetty instead of Tomcat
Basically excluding Tomcat and adding dependency for Jetty.Running mvn clean install from the command line or running the main method resulted in the following exception:
Caused by: javax.validation.ValidationException: HV000183: Unable to
load 'javax.el.ExpressionFactory'. Check that you have the EL
dependencies on the classpath, or use ParameterMessageInterpolator
instead
I could solve this by adding the following dependency in the pom.xml:
<dependency>
<groupId>javax.el</groupId>
<artifactId>javax.el-api</artifactId>
<version>3.0.0</version>
</dependency>
I am not directly using any validation related code but I suspect this is getting pulled from the spring boot jpa starter which pulls in Hibernate. I have also seen a discussion around this here: Similar issue
Questions:
1) Is this the right fix?
2) If it is the right fix, should the documentation be updated to add this dependency as well?

You are correct in using the javax.el dependency. When the JPA pulls in Hibernate as you stated, it will use the Hibernate Validator. It's specified here. This is the right fix. As for the documentation, I personally would raise it but I suspect not everyone will have the same issue. I still can run my mvn clean install without errors however if I run mvn spring-boot:run it starts up and shuts-down straight after.

Related

Spring Boot ClassNotFoundException org.springframework.core.metrics.ApplicationStartup

I am currently playing with some proof-of-concept work in Spring Boot and GCP data storage.
My pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-gcp-data-datastore</artifactId>
<version>1.2.6.RELEASE</version>
</dependency>
Issue: Spring Boot fails to start
When I attempt to launch the application, I get:
Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/core/metrics/ApplicationStartup
at org.springframework.boot.SpringApplication.<init>(SpringApplication.java:251)
at org.springframework.boot.SpringApplication.<init>(SpringApplication.java:264)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1309)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1298)
What I tried
I tried adding the Actuator dependency.
But that did not do the trick.
I cannot figure out what dependency I am missing. I see the class definition here in 5.3.0-M2 documentation, but I'm not sure what dependency it exists in.
I also tried adding following metrics dependencies:
spring-cloud-gcp-starter-metrics
spring-metrics
spring-cloud-stream-metrics
I searched in findjar.com with no luck.
I wouldn't mind disabling it as well if that is possible.
Update:
I added:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.3.1</version>
</dependency>
Which gives me a new error:
An attempt was made to call a method that does not exist. The attempt
was made from the following location:
org.springframework.boot.SpringApplication.run(SpringApplication.java:324)
The following method did not exist:
'void org.springframework.context.ConfigurableApplicationContext.setApplicationStartup(org.springframework.core.metrics.ApplicationStartup)'
The method's class,
org.springframework.context.ConfigurableApplicationContext, is
available from the following locations:
...
Action:
Correct the classpath of your application so that it contains a
single, compatible version of
org.springframework.context.ConfigurableApplicationContext
I was able to solve this by downgrading Spring Boot:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.3.3.RELEASE</version>
</dependency>
Guess it's just not compatible with 2.4.0 yet.
Specifically I also had to ensure that I used 2.3.3.RELEASE and not anything more recent due to other issues I ran across.
I don't have the spring-cloud dependency, but when upgrading to Spring Boot 2.4 from 2.3 I got the same error.
Exception in thread "main" java.lang.NoClassDefFoundError:
org/springframework/core/metrics/ApplicationStartup
Per conversation in this thread (https://github.com/spring-projects/spring-boot/issues/24880) I upgraded to Spring Framework 5.3.3 and it fixed the problem.
I had the same issue caused by an old version of spring-context JAR file loaded instead of the appropriate version.
Ensure that there were no reminiscence of old libraries in your classpath.
I had a similar issue, and it was resolved by adding the following maven dependency:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>5.3.8</version>
</dependency>
I was having the same issue but with:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.2.9.RELEASE</version>
<scope>test</scope>
</dependency>
And the error disappeared after I upgraded to:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.3.3</version>
<scope>test</scope>
</dependency>
May this help, upgrade spring-cloud to the latest version.
This works for me when I faced your first error :
Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/core/metrics/ApplicationStartup
See the twitter thread here, thanks to Stéphane and Arnaud for the help.
I had the same problem, but that was caused by spring-security-ldap that i also import in version 5.4.x
Upgrade spring-security-ldap to version 5.5.0 solved the problem for me.
I had this issue when adding a Spring Boot test to a library for the first time. In my case, the library did not have a Spring Boot application.
This did the trick:
#SpringBootApplication
public class TestApplication {
public static void main(final String[] args) {
SpringApplication.run(TestApplication.class, args);
}
}
Using only spring-boot-starter-test as a Spring dependency.
Check if another dependency doesn't have dependency to spring-core with older version
May this will help someone, who is facing this error.
=> Error should look like this:
Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/core/metrics/ApplicationStartup
at org.springframework.context.support.AbstractApplicationContext.<init>(AbstractApplicationContext.java:229)
at org.springframework.context.support.GenericApplicationContext.<init>(GenericApplicationContext.java:112)
at org.springframework.context.annotation.AnnotationConfigApplicationContext.<init>(AnnotationConfigApplicationContext.java:67)
at org.springframework.context.annotation.AnnotationConfigApplicationContext.<init>(AnnotationConfigApplicationContext.java:91)
Solution to the Problem:-
Go to Project folder\ Right click\ Maven\ Update maven project.
Check your Spring version (pom.xml-> view the properties).
Change the Spring version to an older/new version..Otherwise use this
<properties>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<spring.version>5.2.0.RELEASE</spring.version> <!--add this version-->
</properties>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
<dependency>
<groupId>org.springframework</groupId> <!-- sql dependency for jdbc-->
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
After adding above things, Save it(Please wait for the code to build..).
Run your Program
All The Best Guys, Let's Move Togther
I had the same Spring Boot ClassNotFoundException ApplicationStartup issue, the solution was for me to ensure that the spring boot version in my gradle plugin is the same than the dependencies in my build.gradle.kts like:
plugins {
id("org.springframework.boot") version "2.5.3"
...
}
dependencies {
implementation("org.springframework.boot:spring-boot:2.5.3")
...
}

Spring boot 2.1.7 having tomcat-embed-core conflit

I am migrating existing Spring project into Spring boot.unable to run spring boot application its showing following error.
The error log says there is a conflict on tomcat-embed-core.
In eclipse Dependency hierarchy of porm.xml is given below
i exclude the maven architect ,and try to run the application its showing following error
porm.xml
<modelVersion>4.0.0</modelVersion>
<artifactId>MyService</artifactId>
<packaging>jar</packaging>
<properties>
<java.version>1.8</java.version>
<!-- 2.1.3.RELEASE -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<springframework.boot.version>2.1.7.RELEASE</springframework.boot.version>
</properties>
<name>MyService</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.sybase.jdbc3.jdbc</groupId>
<artifactId>jconn3</artifactId>
<version>${jconn3.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-juli</artifactId>
<version>${tomcat.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jdbc</artifactId>
<version>${tomcat.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
what was wrong in this porm.xml
Where is
${tomcat.version}
defined?
That version probably does not match the tomcat version that auto magically is included with spring boot items.
And thus the conflict.
Go here:
https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web/2.1.7.RELEASE
And start following the COMPILE dependencies, and you'll find the versions that are auto included with 2.1.7.RELEASE. and you have to alter the other includes that are overwriting the springboot auto include tomcat versions.
Again, follow the COMPILED dependency trail.
So below is what you should find by crawling the COMPILED dependency trail (from immediately above in my answer)
https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-tomcat/2.1.7.RELEASE
And you'll find you need to set
tomcat.version to
9.0.22
By defining tomcat.version as 8.x, you are breaking it.
Another way to put it
You have to go ~way~ back to springboot 1.5.2.RELEASE or 1.5.3.RELEASE
https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-tomcat/1.5.2.RELEASE
https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-tomcat/1.5.3.RELEASE
(Again, in the two above links, looked at the COMPILE dependencies)
To find a version of tomcat (that is auto included with springboot) that gets close to tomcat 8.5.x (where 8.5.x is the one you are attempting to use)
That's pretty old.
The principal you are missing is that springboot auto includes dependencies. And anything else you import has to play nice with everything springboot auto includes.
And your current value for tomcat.version is NOT playing nice with everything springboot 2.1.7.RELEASE is auto including.
And now that you've been through all of that. You'll find you'll make your life easier if you engage the springboot world more completely.
Alot of times, springboot will have a (sub)package that will bring in the thing you really desire.
spring-boot-starter-jdbc
https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-jdbc/2.1.7.RELEASE
You would probably be better off bringing that package in, vs hand-picking ones. Aka, get rid of your "tomcat-jdbc" include and see if the spring-boot-starter-jdbc can give you what you want.
The curse/blessing of spring-boot is that it is its own universe. But if you engage, you probably want to play by its rules more often than not.
PS
It is pom.xml, not porm.xml
Try adding spring-boot-starter-tomcat as a dependency:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
Remove tomcat-juli and tomcat-jdbc dependencies. If you need JDBC support, add the corresponding starter:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
If you use JSP views, you will probably need the following dependencies as well:
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
Also, pay attention to your dependencies versions. Spring Boot's parent POM defines version management for many common artifacts so you don't need to set the <version></version> for these libraries. See https://docs.spring.io/spring-boot/docs/2.1.7.RELEASE/reference/htmlsingle/#appendix-dependency-versions

How does Spring Boot load changes in code without restarting the server

This was an interview question to me that , is it possible that some changes you made in your code and it is an spring boot application, and without restarting the server you are able to get those changes.?
if yes, then how is it possible in spring boot.
I want to know that how is it possible in Spring Boot.?
Add spring-boot-devtools module to your project, which includes LiveReload server which can be used to trigger a browser refresh whenever a resource has been changed.You can download browser extensions from livereload.com.
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>
You only need to add devtool dependency in pom.xml and yml file property:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
# for not restarting the server every time
spring.devtools.restart.enabled: false
you may want to check here spring dev tool
Notice:
no matter what, when you have change to your java code, the server need to be restarted, spring dev tool just help you to reload it
if it is jsp then you do not have to restart server.
I'm using intellij, the follow dependency setting works for me.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
Try the following steps and it should work
Add the following to pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
In application.properties
spring.devtools.restart.enabled=true
Note : Ensure spring boot is restarted once after making changes to the application.properties file.
For additional information please check
https://docs.spring.io/spring-boot/docs/1.5.16.RELEASE/reference/html/using-boot-devtools.html

Log4J2 in Spring Boot with embedded Tomcat

I am trying to let the embedded Tomcat in Spring Boot log to my Log4J2 configuration, but it doesn't work.
According to this answer that copes with an external Tomcat: https://stackoverflow.com/a/28639068/1845463 there seems to be the need to move some jars to $Catalina_home/libs etc. I think this is not possible with Spring Boot, isn't it?
Has someone managed to get log4j2 running and be able to configure appenders for catalina log?
Thanks in advance
EDITED: The simplest way to do is to add spring-boot-starter-log4j2.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
N.B. Make sure that the other components don't need different version of log4j. This may cause run-time errors. e.g. elasticsearch java API requires 2.6+ and spring-boot-starter-log4j2:1.3.8 provides log4j:2.4.1, if we're building an app that connects elasticsearch and uses spring boot too. Then we will end up getting NoSuchMethodError or similar errors. For resolution of those errors we should add log4j2:2.6+ in our pom.
Which Spring Boot version are you using? I believe 1.4.x.RELEASE uplifted it to log4j2.
As #M.Deinum mentioned including:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
brings in:
log4j-core, log4j-api and a few more. See https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-log4j2/1.4.6.RELEASE
It might be advisable to exclude logging starter using:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>

Spring Boot WAR jolokia integration

According to the Spring Boot 1.2.3 Reference Docs.
Enabling jolokia seems to be as simple as adding as adding the following Maven dependency:
<dependency>
<groupId>org.jolokia</groupId>
<artifactId>jolokia-core</artifactId>
</dependency>
While this does work for a Spring Boot application packaged as a fat jar, I am unable to get this to work when packaged as a WAR file.
The root cause appears to be:
Caused by: java.lang.ClassNotFoundException: org.json.simple.JSONAware
I'm using STS for development purposes and deploying to an embedded pivotal tc Server 3.1. The dependency(json-simple-1.1.1.jar) containing the org.json.simple.JSONAware does appear under the Maven Dependency node so I'm not sure what the issue is.
So as I was composing the question I stumbled onto a solution that at least seems to work for me:
I took a look at the effective POM and found this dependency declaration:
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
<optional>true</optional>
</dependency>
So for lack of better option I declared the following dependency explicitly
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<optional>false</optional>
</dependency>
Adding false to the the <optional> element seemed necessary.
Now I can access jolokia via the following url:
http://<myurl>:<myport>/<appcontext>/jolokia
Looking at 1.4.4 this seems to have been fixed:
<dependency>
<!-- Make json-simple non-optional.
It is marked optional in boot-dependencies, but required by jolokia-core.
Without this fix it would be missing when used war-packaging. -->
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<optional>false</optional>
</dependency>
Yet I'm seeing similar issues running a war in JBoss.

Resources