The last Spring boot validation not working - java

I'm a beginner in spring boot and I found a problem with validation It doesn't work and I tried spring validation also hibernate dependency. I have the last version of spring boot 2.7.2
I add
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-validation</artifactId> </dependency>
to my pom.xml but when I add #NotNull to my bean gave me an error, and showed me there's not a #NonNull Annotation.

I faced the same issue. In my case, there was a problem with my pom. I had a dependency on the hibernate-validator. So I removed and worked for me.

Related

How to find out which dependency version I should use?

I'm implementing a new feature in my project, in this case replacing the resttemplate with the webclient. I put this dependency in my pom.xml:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
<version>???</version>
</dependency>
I used the mvn dependecy:tree command to find out which version of spring-web was current and I verified that it was 3.2.8-RELEASE. How to find out which spring-boot-starter-webflux version I should use in my project's maven so I don't have dependency issues like this: java.lang.NoClassDefFoundError: org/springframework/http/client/reactive/ClientHttpConnector
In other words, how do I find out which version of spring-boot-starter-webflux is compatible with the version of spring-web I'm using?
if I omit the version, when building, I get the error dependencies.dependency.versin is missing.
Spring Boot includes dependency management tools to help solve this problem. If you're using any Spring Boot dependencies, the versions of all your Spring and Spring Boot dependencies should ideally be managed by Spring Boot.
A simple way to do this is to apply the Spring Boot BOM (Bill of Materials) in your Maven dependency management.
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>3.0.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
Replace the version here with the version of Spring Boot that you want to use. You can then omit the version number from any other Spring or Spring Boot dependencies.
The Spring Boot team maintains the bill of materials so that it provides compatible versioning for all the Spring and Spring Boot components as well as other libraries that the Spring framework depends on.
You can find more information in the docs here: https://docs.spring.io/spring-boot/docs/current/reference/html/using.html#using.build-systems.dependency-management
If you would like to refer to Spring's version compatibility table manually, it's published in the Spring Boot docs. For example, you can find the compatible dependency versions for the current version of Spring Boot here: https://docs.spring.io/spring-boot/docs/current/reference/html/dependency-versions.html
A good place to start if you're trying to identify the compatible Spring Boot version for an existing dependency is to compare their release dates. Look for a Spring Boot version that was released around the same time as your dependency. Then you can look at the compatibility table for that Spring Boot version. By using that method, I couldn't find a version of Spring Boot that's compatible with spring-web 3.2.8. That version of spring-web is over 8 years old and predates the first release of Spring Boot. You should upgrade it, if you can.

Failed to configure Datasource on spring data jpa dependency?

I have a client application with a class that uses Specification, therefore I need a spring data jpa dependency. I know that the Boot dependency:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
...throws an error (Failed to configure a Datasource...) at startup if you include the dependency without a Datasource configured. However, I still get the error when I use this dependency (so just Spring, no Boot) when I import my client in a Spring Boot application when the client uses this dependency:
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
</dependency>
Why do I still get this error? I know I can suppress the warning with a property (exclude DataSourceAutoConfiguration) or annotation, but I would prefer not to, and don't really understand why I get this error from Spring. In my understanding, Boot is opiniated throws this error to inform me of missing config, but I thought the vanilla Spring dependency would not do this. I can't find any info on expected behavior on the non-starter dependency, just on the starter dependency.
In spring boot 2.7.3 I didn't find this problem.

Unable to upgrade swagger2 version from 2.7.0 to 3.00 in Spring REST API(regular Spring not sprint boot)

My project is working fine with swagger2 version 2.7.0.not spring boot(spring mvc only)
I am going to upgrade it 3.0.0 but it's giving plugin errors.
java.lang.nosuchMethodError:org.springframework.plugin.core.pluginRegistry.getPluginFor(Ljava/lang/Object;)Ljava/util/Optional;
My config as below:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>3.0.0</version>
</dependency>
Update: As mentioned in https://github.com/springfox/springfox
Step 2 : Add #EnableOpenApi for open API (and #EnableSwagger2WebMvc or #EnableSwagger2WebFlux for older versions)
I added #EnableSwagger2WebMvc in class ,Then it's asking dependency of springfox-swagger2,
as this coming from :
http://springfox.github.io/springfox/javadoc/snapshot/springfox/documentation/swagger2/annotations/EnableSwagger2WebMvc.html
which is part of springfox-swagger2 dependency
Hence I am confused what to do..
can you please help cleaner approach with steps .
If I don't use #EnableSwagger2WebMvc/EnableSwagger2
Swagger is not getting loaded
Not sure if you are using Spring Boot, or regular Spring flavor, but have you followed the migration instructions posted here?
https://github.com/springfox/springfox
Remove explicit dependencies on springfox-swagger2
Remove any #EnableSwagger2... annotations
Add the springfox-boot-starter (Spring boot) or springfox-oas(Spring MVC) dependency
Check if you do not rely on removed dependecy on guava
If you are using WebMvc but you don't use the #EnableWebMvc annotation yet, add this annotation (Spring boot).

Missing artifact net.sf.ha-jdbc:ha-jdbc:jar:3.0.0-rc-1

We are trying to switch databases for our Spring boot application, in case one fails. We are using hibernate as our ORM. So we found this link Primary/Secondary failover DataSource in Spring Boot, which redirected to Using HA-JDBC with Spring Boot.
But when we tried to add the dependency for HA-JDBC, we were getting error like Missing artifact net.sf.ha-jdbc:ha-jdbc:jar:3.0.0-rc-1, this was the location MVNRepository. We tried with all the versions but the error is same. We tried cleaning the project and updating the project but the error is still there.
This is the dependency
<!-- https://mvnrepository.com/artifact/net.sf.ha-jdbc/ha-jdbc -->
<dependency>
<groupId>net.sf.ha-jdbc</groupId>
<artifactId>ha-jdbc</artifactId>
<version>3.0.0-rc-1</version>
</dependency>

"NoSuchMethodError getHttpServletMapping" after springboot upgrade

I upgraded from springboot 2.1.3 to 2.2.0. So far things works fine but I noticed when I make a rest request that returns a 400, instead of getting the json response I get the error:
[Tomcat].[localhost] : Exception Processing ErrorPage[errorCode=0, location=/error]
java.lang.NoSuchMethodError: javax.servlet.http.HttpServletRequest.getHttpServletMapping()Ljavax/servlet/http/HttpServletMapping;
The funny thing is I get this only when I start the app from Intellij using an emdedded tomcat. (create a mvn profile with "spring-boot:run")
So,
With standalone tomcat, it works fine everywhere
With embedded tomcat, and only if I start from Intellij I get this error.
But I can do the same thing from command line with
mvn spring-boot:run
which then I have no error ?! As suggested on another post I upgraded my IntelliJ to latest version but didn't help
i think you have to upgrade the version of ur tomcat emdedded , there is a version mismatch.
Spring Boot 2.1.X uses Tomcat 9 which has the Servlet API v4.
But Spring Boot Web 2.1.X still incorporates Servlet API v3.1.
OR
Change tomcat version proprety
<properties>
<tomcat.version>8.5.37</tomcat.version>
<properties>
NB:The tomcat.version property is a normal Maven property in your pom.xml. Just add the tomcat.version to your existing Maven properties
I fixed the problem.
Changing property tomcat.version didn't help, so I omitted it and added this to the child pom (trick is it does not work in parent pom). Also note that the version is 2.2.4 and not 2.2.0
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<version>2.2.4.RELEASE</version>
</dependency>
Just wanted to mention another "solution", because I just faced the same problem, after upgrading to Spring Boot 2.4.0
My App is running on Java 11, Spring Boot 2.4.0 AND JakartaEE Api 8.0.0
For a long time I ignored the Jakarta Version, because I found nothing regarding my problem and this dependency in the web.
But after upgrading JakartaEE to 9.0.0 everything worked out fine.
<dependency>
<groupId>jakarta.platform</groupId>
<artifactId>jakarta.jakartaee-api</artifactId>
<version>9.0.0</version>
</dependency>
That was the solution for my problem :) maybe it helps others.

Categories