java - ResponseEntity Api 在 REST API 中添加自定义错误消息
问题描述
我目前正在使用 Spring Rest Api,并且我正在使用 ResponseEntity API 向调用者返回响应。
我想知道如何使用以下配置呈现带有错误消息的响应:
@GetMapping
ResponseEntity<Page<Users>> getUsers(){
try {
usersValidationService.validate(researchCriteria);
}catch(ValidationException e){
return ResponseEntity.noContent().build();// here i want to return an empty list with the custom error message
}
return ResponseEntity.ok(repoUsers.findAll(criteresRecherche.toSpecifications(service), page));
}
解决方案
而不是使用
return ResponseEntity.noContent().build();
试试下面:
<Page<Users>> list = ... // create a empty list and add custom message according to your class <Page<Users>>
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(list);
推荐阅读
- import - 更改导入参数功能模块abap的值
- maven - 如何使用 p2-maven-plugin 包含到本地 p2-site 非 JAR 文件?
- ios - Flutter 无法构建 IOS 存档
- c# - Blazor 级联组件防止 StateHasChanged
- relationship - 使用 DirectQuery 在 PowerBI 中转换表列名称会删除该表的所有关系
- java - 根据登录用户和共享模式在运行时更改数据库模式
- python - 我正在尝试更新 tkinter 中的场景,我该怎么做?
- java - LinkedList 新的反向算法
- doctrine-orm - 如何选择在多对多关系中同时具有这两个项目的行
- xpath - 通过只知道根节点名称来提取孙子节点的父节点的 Xpath local-name