2016-10-25 48 views
0

我有一个使用Spring Boot构建的API。默认情况下,Spring引发错误时的默认JSON结构是;更改Spring Boots默认JSON错误响应结构

{ 
    "timestamp": 1477425179601, 
    "status": 404, 
    "error": "Not Found", 
    "message": "No message available", 
    "path": "/categoriess" 
} 

这种结构是不同的错误响应的API在返回自己,所以我想改变Spring使用相同的结构,我自己的一致性。

我的错误响应是这样构造的;

{ 
    "errors": [ 
     { 
      "code": 999404, 
      "message": "The resource you were looking for could not be found" 
     } 
    ] 
} 

我该如何去做这件事?我试过使用异常处理程序,但我无法弄清楚设置它的正确例外。我也想确保Http状态仍然正确返回为404,或者错误是(500等)。

回答

0

执行此类操作的一种可能方法是使用@ExceptionHandler注释在控制器内部创建处理程序方法。

@RestController 
@RequestMapping(produces = APPLICATION_JSON_VALUE) 
public class MyController { 

    @RequestMapping(value = "/find", method = GET) 
    public Object find() { 
     throw new UnsupportedOperationException("Not implemented yet!"); 
    } 

    @ExceptionHandler 
    public ErrorListModel handleException(Exception exception) { 
     ExceptionModel exceptionModel = new ExceptionModel(1337, exception.getMessage()); 

     ErrorListModel list = new ErrorListModel(); 
     list.add(exceptionModel); 

     return list; 
    } 

    private class ErrorListModel { 
     private List<ExceptionModel> errors = new ArrayList<>(); 

     public void add(ExceptionModel exception) { 
      errors.add(exception); 
     } 

     public List<ExceptionModel> getErrors() { 
      return errors; 
     } 
    } 

    private class ExceptionModel { 
     private int code; 
     private String message; 

     public ExceptionModel(int code, String message) { 
      this.code = code; 
      this.message = message; 
     } 

     public int getCode() { 
      return code; 
     } 

     public String getMessage() { 
      return message; 
     } 
    } 
} 

的私有类​​和ExceptionModel只是帮助确定所产生的JSON体应该如何看,我假设你已经有了自己的,类似的类。

find方法只是抛出一个异常给我们处理,它被handleException方法拦截,因为它的注释是@ExceptionHandler。在这里,我们创建一个ExceptionModel,用来自原始异常的信息填充它,然后将其添加到​​,然后我们返回。

This blog post从2013年开始解释功能比我以前更好,它还提到了一个额外的选项,@ControllerAdvice。它基本上允许您在其他控制器中重新使用异常处理。

+0

谢谢。在我的项目中,我有一个使用ControllerAdvice注释的ExceptionConntroller,在那里放置了ExceptionHandlers。我已经尝试创建一个异常处理程序,但遗憾的是我似乎无法捕捉到Spring标准404 Not Found响应。我试过上面的例子,它也没有抓住它。 – SheppardDigital

+0

然后这篇文章可能会为您解答一些问题:http://stackoverflow.com/questions/28902374 –

0

我又看了一眼,确实设法把一些东西放在一起,这对我很有用。

@Bean 
    public ErrorAttributes errorAttributes() { 
     return new DefaultErrorAttributes() { 
      @Override 
      public Map<String, Object> getErrorAttributes(RequestAttributes requestAttributes, boolean includeStackTrace) { 
       Map<String, Object> errorAttributes = super.getErrorAttributes(requestAttributes, includeStackTrace); 

       Map<String, Object> error = new HashMap<>(); 
       error.put("code", errorAttributes.get("status")); 
       error.put("message", errorAttributes.get("error")); 

       Map<String, Object> errorResponse = new HashMap<>(); 
       errorResponse.put("errors", error); 

       return errorResponse; 
      } 
     }; 
    } 

这将返回以下JSON响应以及Spring将返回的任何标头/ http状态码。

{ 
    "errors": { 
    "code": 404, 
    "message": "Not Found" 
    } 
} 

这似乎是工作伟大的弹簧产生的错误,而我自己的异常,我在处理控制器或与的ExceptionHandlers特定ControllerAdmin类。