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 .
Related
I have a java spring boot graphql project.
My dependencies in the pom.xml:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.graphql-java-kickstart</groupId>
<artifactId>graphql-spring-boot-starter</artifactId>
<version>12.0.0</version>
</dependency>
<dependency>
<groupId>com.graphql-java-kickstart</groupId>
<artifactId>playground-spring-boot-starter</artifactId>
<version>5.10.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
When i run the application and visit http://localhost:3001/playground i get an empty page saying
"Loading GraphQL Playground"
What may be possibly my problem here.
You can use only this dependency for graphQL playground interfaces and remove the playground-spring-boot-starter:
<dependency>
<groupId>com.graphql-java-kickstart</groupId>
<artifactId>graphql-spring-boot-starter</artifactId>
<version>12.0.0</version>
</dependency>
In your application.yml you need to explicit set the static path for interface files (the path is /vendor/playground/):
static-path:
base: <YOUR-CONTEXT-PATH>/vendor/playground/
Here is a example of some options to enable GraphQL playground, notice that the context-path here is /api:
graphql:
playground:
endpoint: /graphql
subscriptionEndpoint: /subscriptions
enabled: true
pageTitle: Playground
cdn:
enabled: false
version: latest
static-path:
base: /api/vendor/playground/
I had the same issue and the settings above worked for me.
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'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).
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.
I'm building a library where I scan a class and check if their field as OneToMany and ManyToOne annotations. I currently added eclipselink 3.6 as dependecy of my module, like this
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
<version>2.6.0</version>
</dependency>
but I don't want to make my lib dependent on eclipselink, I want it able to be used with any JPA implementation. How do i do that?
Unfortunately, there's no standard package that provides only the annotations/interface (like, for example, on the servlet spec). Each ORM has their own package, but they all follow the jpa standard. What you can do is declare the dependency as optional.
For Eclipselink
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>javax.persistence</artifactId>
<version>2.1.0</version>
<optional>true</optional>
</dependency>
Probably you'll need to depend on eclipselink for your tests, so you can mark the original dependency only for test...
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
<version>2.6.0</version>
<scope>test</scope>
</dependency>