I'm trying to use javax constraints to pre-validate a request content before running the logic.
I have tried any possible solution out there but still can't put javax annotations to work in a Spring boot.
import javax.validation.constraints.Min
import javax.validation.constraints.Pattern
data class LoginRequest (
#Credential //Custom constraint that works just fine
val credential: String,
#Min(value= 5)
val password: String,
#Pattern(regexp = Constants.Regex.DEVICE_ID_REGEX, message = "Invalid device ID")
val device: String
): Serializable
This is a part of pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
.
.
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>2.0.1.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.0.17.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator-annotation-processor</artifactId>
<version>6.0.17.Final</version>
</dependency>
And this is the controller
import javax.validation.Valid
#PostMapping("/login")
fun userLoginEndpoint(#Valid #RequestBody loginRequest: LoginRequest): ResponseEntity<User> {
return authService.loginUser(loginRequest)
?.let{ ResponseEntity(it, HttpStatus.ACCEPTED)}
?: ResponseEntity.status(HttpStatus.UNAUTHORIZED).build()
}
Am I doing something wrong?
From #JBNizet comment
Replace #Min(value= 5) (and the other validation annotations too, of course) by #field:Min(value= 5).
Related
Adding #NotBlank annotation to member variables in DTO does not take effect. What is the reason? Please help me
first check this dependency is present in the pom.xml file. If it doesn't present then add.
With dependency present in your project now use can use #NotBlack and #Valid annotation.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.197</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
For more information about Validation in Spring Boot check this link
Apart from including spring-boot-starter-validation in dependency, you must include #Valid annotation on #RequestBody in the controller for #NotBlank annotation to apply on member variables in DTO .
Hello after spending hours reading stackoverflow answers and trying to find the answer, I'm still unable to find out why validators such as NotNull and NotBlank are not working in java. I can still send post requests with a blank name. The project is on Java 11 and Spring boot starter v2.5.3. Thank you very much in advance if you are able to figure it out, I feel I must buy you an ice cream :')
In my model:
import javax.validation.constraints.NotBlank;
#NotBlank
private final String name;
In my controller:
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
#PostMapping(path="add")
public void addUser(#Valid #NotNull #RequestBody User user) {
//Request body takes the data taken and converts it into a User
userService.addUser(user);
}
#PutMapping(path="user/{id}")
public void updateUser(#PathVariable("id") UUID id, #Valid #NotNull #RequestBody User userToUpdate) {
userService.updateUser(id, userToUpdate);
}
In my dependencies in pom.xml:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
</dependency>
</dependencies>
I had the same issue, but I could not find out the reason. the only solution was to use older JDK. I change to JDK1.8, and it is working fine.
I had the same error. I excluded 'hibernate-validator' dependency which was coming from some other dependency (you can see this by the dependency tree) and Added these 2 dependencies and it worked fine.
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>2.0.1.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.0.13.Final</version>
</dependency>
I have a Java 1.7 starts application. It does have some RESTful api's.
I would like to add #JsonIgnore to a filed so that it is not returned in the api.
E.g.
Member.java
import com.fasterxml.jackson.annotation.JsonIgnore;
private java.lang.String username;
#JsonIgnore
private java.lang.String password;
Does not ignore the password.
"member": {
"password": "**************",
"username": "richard"
}
I think the reason why #JsonIgnore does not work, is because I use com.fasterxml.jackson.annotation.JsonIgnore. Should I use a different annotation from a different library? i.e. Is my implementation of jaxrs maybe not com.fasterxml.jackson? How do I tell?
The IntelliJ classpath has:
(I have tried net.minidev.json.annotate.JsonIgnore with no success)
More info:
pom.xml
<dependency>
<groupId>net.minidev</groupId>
<artifactId>json-smart</artifactId>
<version>2.3</version>
</dependency>
and
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.11.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.11.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.11.2</version>
</dependency>
Is this the problem? two versions! I am not sure where the 2.0.5 version comes from, as it is not defined in the pom.
You don't need a #JsonIgnore in this case. You can simply omit the variable that you don't want deserialized(the password in this case) and jackson will just return you the username.
I have a Spring Boot console application that uses Spring Data.
I have a simple jpa repository
public interface MyRepository extends JpaRepository<MyEntity, Integer> {
}
and an entity defined like this
import javax.validation.constraints.Min;
// ....
#Entity
#Table(schema = "mySchema", name = "myTable")
public class MyEntity {
// ...
#Column(nullable = false)
#Min(100)
private Integer group;
// ...
}
When I save this entity
myRepository.save(myEntity);
pom:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.8.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>2.0.1.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.1.0.Final</version>
</dependency>
</dependencies>
I expect to see a validation error because group is 0 but I don't see any issue and at the database the value of a newly added row is 0. What else do I need to do to enable validation?
Try with import javax.validation.Valid; #Valid
When the target argument fails to pass the validation, Spring Boot throws a MethodArgumentNotValidException exception.
Adding this to pom did the trick
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
I am trying to integration Spring 4.2.3 with hibernate validator v.5.2.2.Final to validate input JSON data expose with REST Controller.
I don't see any compile time or running time exception but at the same time it does not validate input data.
Pom.xml
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.3.RELEASE</version>
<scope>compile</scope>
</dependency>
<!-- jsr303 validation dependencies -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.2.2.Final</version>
</dependency>
<dependency>
<groupId>javax.el</groupId>
<artifactId>javax.el-api</artifactId>
<version>2.2.4</version>
</dependency>
<dependency>
<groupId>org.glassfish.web</groupId>
<artifactId>javax.el</artifactId>
<version>2.2.4</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator-cdi</artifactId>
<version>5.2.2.Final</version>
</dependency>
Pojo java class:
public class RequestVO {
#NotEmpty
private String testValidation;
#NotNull
private String testNull;
#NotBlank
private String testBlank;
.... getters and setters .....
}
#Controller
#RequestMapping("/login")
public class LoginController {
#RequestMapping(method=RequestMethod.POST, value="/user")
public #ResponseBody ResponseVO getLoginResponse(#Valid #RequestBody RequestVO request, Errors error) {
ResponseVO response = new ResponseVO ();
if (error.hasErrors()) {
System.err.println("Success");
}
return response;
}
}
In your dependency list there's a comment saying ...jsr303 validation dependencies.... Note the bean validation has an updated specification (JSR-349), and this can very well be your issue.
You should align your dependencies in the following manner
hibernate-validator-5.x.x
validation-api-1.1.x
which implement JSR-349
OR
hibernate-validator-4.x.x
validation-api-1.0.x.
which implements JSR-303
If you mix the dependencies, the validation will simply not kick-in. As a solution suggestion, add
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.1.0.Final</version>
</dependency>
and make sure that you remove any dependency to validation-api-1.0 jar
I think is interest to take a look at this link
and if you to remove the validation just remove #Valid from method and you're ok.