Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java SpringDataREST在哪里生成异常JSON应答?_Java_Spring_Spring Mvc_Spring Data Rest - Fatal编程技术网

Java SpringDataREST在哪里生成异常JSON应答?

Java SpringDataREST在哪里生成异常JSON应答?,java,spring,spring-mvc,spring-data-rest,Java,Spring,Spring Mvc,Spring Data Rest,我使用的是SpringBoot1.5.3、SpringDataREST、SpringHateoas和Hibernate。 Spring Data REST以非常好的方式管理异常,返回格式良好的JSON对象,如下所示: { "timestamp": "2017-06-24T16:08:54.107+0000", "status": 500, "error": "Internal Server Error", "exception": "org.springf

我使用的是SpringBoot1.5.3、SpringDataREST、SpringHateoas和Hibernate。 Spring Data REST以非常好的方式管理异常,返回格式良好的JSON对象,如下所示:

    {
    "timestamp": "2017-06-24T16:08:54.107+0000",
    "status": 500,
    "error": "Internal Server Error",
    "exception": "org.springframework.dao.InvalidDataAccessApiUsageException",
    "message": "org.hibernate.TransientPropertyValueException: Not-null property references a transient value - transient instance must be saved beforeQuery current operation : com.test.server.model.workflows.WorkSession.checkPoint -> com.test.server.model.checkpoints.CheckPoint; nested exception is java.lang.IllegalStateException: org.hibernate.TransientPropertyValueException: Not-null property references a transient value - transient instance must be saved beforeQuery current operation : com.test.server.model.workflows.WorkSession.checkPoint -> com.test.server.model.checkpoints.CheckPoint",
    "path": "/api/v1/workSessions/start"
}
我需要本地化异常消息,并希望保持SpringDataREST的JSON格式不变,看看它们是如何创建异常对象的。 我正在寻找在源代码中创建异常的代码,但我找不到。可能是有用的,但它没有最终到达用户的对象的结构


创建异常的点在哪里?

最后,在我没有看到之前,我找到了一个有用的链接:


所以我要找的对象在这里:

有一个答案,来自有用的链接

如错误处理文档中所述,您可以提供 您自己的bean,它实现ErrorAttributes来控制 内容

以下是文档中的示例:

@ControllerAdvice(basePackageClasses = FooController.class)
public class FooControllerAdvice extends ResponseEntityExceptionHandler {

    @ExceptionHandler(YourException.class)
    @ResponseBody
    ResponseEntity<?> handleControllerException(HttpServletRequest request, Throwable ex) {
        HttpStatus status = getStatus(request);
        return new ResponseEntity<>(new CustomErrorType(status.value(), ex.getMessage()), status);
    }

    private HttpStatus getStatus(HttpServletRequest request) {
        Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");
        if (statusCode == null) {
            return HttpStatus.INTERNAL_SERVER_ERROR;
        }
        return HttpStatus.valueOf(statusCode);
    }
  }
@ControllerAdvice(basePackageClasses=FooController.class)
公共类FooControllerAdvice扩展了ResponseEntityExceptionHandler{
@ExceptionHandler(YourException.class)
@应答器
ResponseEntity HandleController异常(HttpServletRequest请求,Throwable ex){
HttpStatus status=getStatus(请求);
返回新的ResponseEntity(新的CustomErrorType(status.value(),例如getMessage()),status);
}
私有HttpStatus getStatus(HttpServletRequest请求){
Integer statusCode=(Integer)request.getAttribute(“javax.servlet.error.status_code”);
if(statusCode==null){
返回HttpStatus.INTERNAL\u SERVER\u错误;
}
返回HttpStatus.valueOf(statusCode);
}
}

只需将Locale注入HandleController异常方法,将MessageSource注入advice,然后在HandleController异常中从异常消息中获取所需的本地化异常消息

,异常似乎是在保存阶段创建的。似乎在这个过程中与一个未真正创建的对象有某种关系DB@AngeloImmediata我不在乎例外本身。我对SpringDataREST如何以这种方式创建JSON对象感兴趣。与异常无关,JSON对象总是这样。我想找到创建该对象的代码。谢谢