简体   繁体   English

Spring 启动验证错误消息未在响应中显示

[英]Spring Boot validation error message not shown in response

I have following simple project to test spring boot validation.我有以下简单的项目来测试 spring 启动验证。 I am using Spring boot version 2.5.6我正在使用 Spring 启动版本 2.5.6

Validation dependency in pom.xml pom.xml 中的验证依赖

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

DTO object 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 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:当我使用null 名称发出请求时,我收到错误响应,但消息丢失:

{
    "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. Spring 引导限制错误响应中包含的信息,以降低将有关应用程序的敏感信息泄露给客户端的风险。 You can explicitly enable additional information in the response by setting some properties in application.properties or application.yml .您可以通过在application.propertiesapplication.yml中设置一些属性来显式启用响应中的其他信息。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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