gpt4 book ai didi

java - 响应中未显示 Spring Boot 验证错误消息

转载 作者:行者123 更新时间:2023-12-05 09:03:12 25 4
gpt4 key购买 nike

我有以下简单项目来测试 spring boot 验证。我使用的是 Spring Boot 版本 2.5.6

pom.xml 中的验证依赖

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

DTO对象

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);
}
}

当我用null name 发出请求时,我收到了错误响应,但消息丢失了:

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

最佳答案

Spring Boot 限制错误响应中包含的信息,以降低将有关应用程序的敏感信息泄露给客户端的风险。您可以通过设置 some properties 在响应中显式启用其他信息。在 application.propertiesapplication.yml 中。

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

关于java - 响应中未显示 Spring Boot 验证错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70212178/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com