简体   繁体   中英

Spring Boot validation error message not shown in response

I have following simple project to test spring boot validation. I am using Spring boot version 2.5.6

Validation dependency in pom.xml

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-validation</artifactId>
</dependency>

DTO object

import javax.validation.constraints.NotNull;

public class DepartmentDTO {

    @NotNull(message = "Department name can not be empty")
    private String name;

    // getter and setter
}

REST Controller

@RestController
public class DepartmentResource {

    @PostMapping("/departments")
    public ResponseEntity<DepartmentDTO> createDepartment(@Valid @RequestBody DepartmentDTO department) {
        return new ResponseEntity<>(department, HttpStatus.OK);
    }
}

When I fire a request with null name I get the error response, but the message is missing:

{
    "timestamp": "2021-12-03T09:13:52.729+00:00",
    "status": 400,
    "error": "Bad Request",
    "path": "/departments"
}

Spring Boot limits the information included in the error response to reduce the risk of leaking sensitive information about the application to a client. You can explicitly enable additional information in the response by setting some properties in application.properties or application.yml .

server.error.include-binding-errors=always
server.error.include-message=always

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM