java - Spring Boot Rest Api 不显示验证消息
问题描述
我正在使用 Spring Boot 开发一个小型 REST API。我在我的实体类中使用验证注释:
@NotEmpty(message="Please enter a first name")
private String firstName;
这是我的控制器和服务:
@RestController
public class CustomerController {
@Autowired
private CustomerService userService;
@PostMapping("/customer")
@ResponseStatus(HttpStatus.CREATED)
public Customer postUser (@RequestBody @Valid Customer customer){
return userService.save(customer);
}
}
public class CustomerService {
@Autowired
private CustomerRepository repository;
@Transactional
public Customer save ( Customer customer){
return repository.save(customer);
}
}
这是我尝试发送带有空 firstName 值的 JSON 时得到的响应:
{
"timestamp": "2020-06-08T08:40:08.660+00:00",
"status": 400,
"error": "Bad Request",
"message": "",
"path": "/MVCwebshop/customer"
}
如您所见,我得到的 JSON 中的“消息”字段为空。当我尝试使用默认消息时也会发生这种情况。我怎样才能解决这个问题?
解决方案
如果使用 Spring Boot 2.3.0.RELEASE 你必须设置
server.error.include-message=always
在您的 application.properties(或 yaml)中。
看这里
推荐阅读
- mongodb - MongoDB 查询以使用另一个日期字段加 1 天更新文档中的日期字段
- javascript - 具有串联 int 的 Discord Bot MySql 查询返回空结果
- javascript - 如何使用css在图像旁边设置表格位置?
- c# - `typeof` 运算符如何在泛型方法中获取类型参数?
- javascript - 从 php 日期数组创建 javascript 日期数组并查看特定值是否匹配
- chart.js - AutoSkip: False 不能在时间 xAxes 标签上工作
- javascript - 为什么我的下拉菜单在导航栏上不起作用?
- javascript - Angular 5,使用 rxjs 计时器,在代码执行期间何时会发出事件?
- java - Java:局部变量的相应内存何时存在?
- javascript - react-admin:调度动作以存储来自动态请求的元数据