Why is Java saying I can't use #NotBlank with a string - java

I have the following code
import javax.validation.constraints.NotBlank;
...
#NotBlank(message="Not Blank")
private String thing;
When I try to run I get
No validator could be found for constraint 'javax.validation.constraints.NotBlank' validating type 'java.lang.String'
How do I use NotBlank annotation with a string?
Java 8

The javax.validation.constraints.NotBlank annotation you are applying is part of Bean Validation API v2.
The most probable case is:
You have bean validation API v2 as dependency on your project
The implementing framework/library supports only v1.0/v1.1 API.
Please check on this post for information about existing implementations: Is there JSR-303 implementation available?
I recommend you checking for bean validation API v2 on your classpath (it also can be called JSR-380). The only implementation of this API I know about is Hibernate Validator v6.0. Make sure that v2 implementation is not hidden by some v1.x implementation being on classpath.

I have encountered this problem and solved it. Because the validation version conflicts,
In my project, the error was introduced twice validate。check the jar package, and then remove, then successful。
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>2.1.0.Final</version>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.1.0.Final</version>
</dependency>

Related

Got error after upgrade spring-web version

I got error after upgrade the spring-web version to after 6.0.0, previous is 5.3.20
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>6.0.4</version>
</dependency>
Cannot access 'status': it is invisible (private in a supertype) in 'ResponseStatusException'
is ResponseStatusException -> {
serverWebExchange.response.statusCode = throwable.status
}
In spring 5.3.x ResponseStatusException used to have getStatus() available directly as you can see in the java doc https://docs.spring.io/spring-framework/docs/5.3.x/javadoc-api/
In version 6.0.x it was changed and ResponseStatusException extends from ErrorResponseException which does not have the getStatus() method directly available.
Instead, you need to call responseStatusException.getBody().getStatus() in order to access the status correctly. Check the java doc for details https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/ErrorResponseException.html
From your question it is not clear if you access status directly in your code or if this code is part of some library. If it is part of the library, then you would need to upgrade the version of the library to the one compatible with spring 6.

Cannot use #NotBlank

I just started working with spring and I'm trying to use #NotBlank. Problem is it is not working. In pom.xml I have this dependency:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
<version>2.7.1</version>
</dependency>
The error I'm getting is this:
This is my import:
import javax.validation.constraints.NotBlank;
and I'm also getting this error:
I have searched similar questions and the answer is that by adding this dependency and restarting IDE will work. In my case it's still not working. Any suggestions?
Add a dependency on javax.validation:validation-api:2.0.1.Final. That defines the validation API (i.e. the interface) but does not implement it. spring-boot-starter-validation has a dependency on hibernate-validator which is the reference implementation of Java's validation API.
You imported the implementation but not the interface which is why your IDE was complaining. By importing both the interface and the implementation that code should compile.

java.lang.IncompatibleClassChangeError: class org.springframework.session.hazelcast.PrincipalNameExtractor

I am trying to configure spring session backed by hazelcast for spring security oauth2 client application.
I followed below link to configure hazelcast.
https://docs.spring.io/spring-session/docs/current/reference/html5/guides/java-hazelcast.html
However, classes annotated with #SpringBootTest started failing due to below error:
java.lang.IncompatibleClassChangeError: class org.springframework.session.hazelcast.PrincipalNameExtractor has interface com.hazelcast.query.extractor.ValueExtractor as super class
My pom.xml of spring boot 2.5 application contain below two new dependencies for hazelcast configuration
<dependency>
<groupId>com.hazelcast</groupId>
<artifactId>hazelcast</artifactId>
<version>4.2</version>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-hazelcast</artifactId>
</dependency>
As per below stackoverflow post , issue seems to be that ValueExtractor is available for two different versions in maven dependencies
IncompatibleClassChangeError: class ClassMetadataReadingVisitor has interface ClassVisitor as super class
Hence, I checked maven dependencies and can see that PrincipalNameExtractor is available from 2.5 version of spring-session-hazelcast while ValueExtractor is available from 4.2 version on com.hazelcast. However,
spring-session-hazelcast is using 3.12.12 version of com.hazelcast for compilation.
So should I use 3.12.12 version of com.hazelcast to resolve this issue or am in misinterpreting the issue? I prefer to use latest version.
Starting from spring-sessions v2.4.0, you can use Hazelcast v4.x with configuring Hazelcast4PrincipalNameExtractor and Hazelcast4IndexedSessionRepository for the session repository. The only difference is the class names with 4 indicator. That is, HazelcastIndexedSessionRepository becomes 4.x compatible with Hazelcast4IndexedSessionRepository.
Here is a guide covering both versions: https://guides.hazelcast.org/spring-session-hazelcast/

Upgrading CXF and WSS4J but can't find dependencies

So I have a working project that I am trying to upgrade CXF and WSS4J
It looks like I have upgraded my client ok, but that's because I am using this for wss4j:
<dependency>
<groupId>org.apache.wss4j</groupId>
<artifactId>wss4j</artifactId>
<version>2.1.8</version>
<type>pom</type>
</dependency>
But my server is throwing ClassNotFound exceptions, obviously because of the type=pom.
So where do I find the new classes jars? Specifically WSPasswordCallback.
BTW, I am using CXF 3.1.9
The short answer is that they moved everything. The first place you want to look is: https://mvnrepository.com/artifact/org.apache.wss4j
That is where all the new ones are. From there you will simply need to google the class you want and see if you can find the package you need. For me WSPasswordCallback moved to the commons package...
<dependency>
<groupId>org.apache.wss4j</groupId>
<artifactId>wss4j-ws-security-common</artifactId>
<version>2.1.8</version>
</dependency>
IMHO this could have been handled better, but it's a start to find what you need.

What is the right place for javax.validation

I am working on a simple project as a point of reference for new projects or to try new technologies. The project has Hibernate & Spring capabilities.
I tried to add some anotations for validate fields. Those are the validation which I am using
import javax.validation.constraints.Digits;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
The problem is that I need javax.validation dependecy and There are a lot of jars which includes those clases but none of Spring or Hibernate.
Which one is the standard?
Note: I check the dependency in the big project and I am using the validation-api
Try the library "Apache Geronimo JSR-303 Bean Validation Spec API", the jar is "geronimo-validation_1.0_spec-1.0-CR5.jar".
Look at this link :
http://www.findjar.com/jar/org/apache/geronimo/specs/geronimo-validation_1.0_spec/1.0-CR5/geronimo-validation_1.0_spec-1.0-CR5.jar.html
Here is maven part to add it :
http://mvnrepository.com/artifact/org.apache.geronimo.specs/geronimo-validation_1.0_spec/1.0-CR5
Use
<dependency>
    <groupId>javax.validation</groupId>
    <artifactId>validation-api</artifactId>
    <version>1.1.0.Final</version>
</dependency>
or
<dependency>
    <groupId>javax.validation</groupId>
    <artifactId>validation-api</artifactId>
    <version>1.0.0.GA</version>
</dependency>
depending on which version of the API you want to use. Note that the API is not enough, of course you also need an implementation (if you don't deploy your application in an Java EE application server). The implementation usually has a transitive dependency on the correct API jar.

Categories

Resources