fil*_*p_j 11 java rest spring-validator spring-boot
我有以下简单的项目来测试 Spring Boot 验证。我使用的是 Spring boot 版本 2.5.6
pom.xml 中的验证依赖项
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)
DTO对象
import javax.validation.constraints.NotNull;
public class DepartmentDTO {
@NotNull(message = "Department name can not be empty")
private String name;
// getter and setter
}
Run Code Online (Sandbox Code Playgroud)
休息控制器
@RestController
public class DepartmentResource {
@PostMapping("/departments")
public ResponseEntity<DepartmentDTO> createDepartment(@Valid @RequestBody DepartmentDTO department) {
return new ResponseEntity<>(department, HttpStatus.OK);
}
}
Run Code Online (Sandbox Code Playgroud)
当我触发具有空名称的请求时,我收到错误响应,但消息丢失:
{
"timestamp": "2021-12-03T09:13:52.729+00:00",
"status": 400,
"error": "Bad Request",
"path": "/departments"
}
Run Code Online (Sandbox Code Playgroud)
Sco*_*ick 15
Spring Boot 限制错误响应中包含的信息,以降低将应用程序的敏感信息泄露给客户端的风险。您可以通过在 或中设置一些属性来显式启用响应中的附加信息。application.propertiesapplication.yml
server.error.include-binding-errors=always
server.error.include-message=always
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7786 次 |
| 最近记录: |