-2
Guys, it’s like this: the validation is occurring, I can see the error message in the console, but in Postman, only the status 400 appears, with empty body, no error message or anything.
Follows the codes:
In the Controller class
public ResponseEntity<User> create(@Valid @RequestBody User user) {
userService.create(user);
return new ResponseEntity<User>(user, HttpStatus.CREATED);
}
In the User class:
@NotBlank(message = "{name.not.blank}")
private String name;
In Gradle
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-web'
compileOnly 'org.projectlombok:lombok'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
runtimeOnly 'org.postgresql:postgresql'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
'org.hibernate:hibernate-core'
implementation group: 'org.springframework.boot', name: 'spring-boot-starter-validation'
}
In console (after sending the request with the empty name field:
2021-08-24 22:29:12.591 WARN 15874 --- [nio-8080-exec-2] .m.m.a.Exceptionhandlerexceptionresolver : Resolved [org.framework.web.bind.Methodargumentnotvalidexception: Validation failed for argument [0] in public org.springframework.http.ResponseEntity<com.github.amandaveredas.adressapi.entity.User> com.github.amandaveredas.adressapi.controller.UserController.create(com.github.amandaveredas.adressapi.entity.User): [Field error in object 'user' on field 'name': rejected value []; codes [NotBlank.user.name,NotBlank.name,NotBlank.java.lang.String,NotBlank]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [user.name,name]; arguments []; default message [name]]; default message [Nome não deve estar em branco!]] ]
That’s it, I don’t know why the message that appears on the console doesn’t appear on Postman.
You need to create an Exception Handler
– Jackson Alves