I'm new to MongoDB. I'm using Spring boot 2.1.3.RELEASE.
I want to perform validation on MongoDB documents (on message as per below code), currently, it saves documents even everything passed as null.
I have gone through a couple of answers like Spring data mongoDb not null annotation like Spring data Jpa
public class Comment {
@NotBlank(message = "Comment's message can't be blank")
private String message;
@CreatedDate
private Date createdDate;
@CreatedBy
private String createdBy;
}
Is there any way to achieve the same without using hibernate-validator dependency?
i was with the same problem and found this question. Here is how i solved my problem in my application that uses Spingboot e MongoDB
this is the only dependency needed.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
The DTO class with the rigth imports
And in the controller i used @Validated from
import org.springframework.validation.annotation.Validated;
@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public ProdutoData inserirProduto(@RequestBody @Validated ProdutoForm produto){
return produtoServico.salvar(produto);
}
I hope this can help someone.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.