java如何向来自我的服务器的json响应添加和修改
所以我有一个REST控制器,我试图捕捉来自非法状态异常的消息
@GetMapping
public List<Student> getAllStudent(){
throw new IllegalStateException("Opps can not get all students");
//return studentService.getAllStudents();
}
在我的前端,当我使用Postman时,我想记录消息“Opps无法获取所有学生”,我得到以下JSON响应:
{
"timestamp": "2021-08-29T22:06:47.477+00:00",
"status": 500,
"error": "Internal Server Error",
"path": "/api/students"
}
如何向JSON添加消息属性,以便在前端使用它
# 1 楼答案
随意抛出异常(
YourselfException
),例如“无法获取所有学生异常” 然后你可以定义一个GlobalExceptionHandler
,然后你可以返回你想要的消息有关详细信息,您可以尝试搜索
"GlobalExceptionHandler"