在Spring MVC或Spring-Boot中返回不同类型的ResponseEntity的最佳方法是什么 [英] What is the best way to return different types of ResponseEntity in Spring MVC or Spring-Boot

查看:645
本文介绍了在Spring MVC或Spring-Boot中返回不同类型的ResponseEntity的最佳方法是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用Spring MVC 4(或Spring-Boot)编写了简单的rest应用程序.在控制器内,我已返回ResponseEntity.但是在某些情况下,我想提供成功的JSON,如果存在验证错误,我想提供错误的JSON.当前,成功和错误响应完全不同,因此我为错误和成功创建了2个类.如果内部逻辑正常,我想在控制器内返回ResponseEntity<Success>.否则,我想返回ResponseEntity<Error>.有什么办法可以做到的.

I have written simple rest application using Spring MVC 4 (or Spring-Boot). Within the controller I have return ResponseEntity. But in some cases I want to give success JSON and if there is validation error I want to give error JSON. Currently success and error responses are totally different, So I have created 2 classes for error and success. Within the controller I want to return ResponseEntity<Success> , if the internal logic is okay. Otherwise I want to return ResponseEntity<Error>. Is there any way to do it.

SuccessError是我用来表示成功和错误响应的2个类.

Success and Error are the 2 classes that i use to represent success and error response.

推荐答案

我建议使用Spring的@ControllerAdvice处理验证错误.阅读本指南,从名为"Spring Boot错误处理"的部分.要进行深入的讨论,请参见一篇文章于2018年4月更新的Spring.io博客中.

I recommend using Spring's @ControllerAdvice to handle validation errors. Read this guide for a good introduction, starting at the section named "Spring Boot Error Handling". For an in-depth discussion, there's an article in the Spring.io blog that was updated on April, 2018.

有关其工作原理的简短摘要:

A brief summary on how this works:

  • 您的控制器方法应仅返回ResponseEntity<Success>.它不负责返回错误或异常响应.
  • 您将实现一个类,该类处理所有控制器的异常.此类将用@ControllerAdvice
  • 进行注释
  • 此控制器建议类将包含用@ExceptionHandler
  • 注释的方法
  • 每个异常处理程序方法将被配置为处理一种或多种异常类型.您可以在这些方法中指定错误的响应类型
  • 在您的示例中,您将在控制器通知类中声明一个用于验证错误的异常处理程序方法.返回类型为ResponseEntity<Error>
  • Your controller method should only return ResponseEntity<Success>. It will not be responsible for returning error or exception responses.
  • You will implement a class that handles exceptions for all controllers. This class will be annotated with @ControllerAdvice
  • This controller advice class will contain methods annotated with @ExceptionHandler
  • Each exception handler method will be configured to handle one or more exception types. These methods are where you specify the response type for errors
  • For your example, you would declare (in the controller advice class) an exception handler method for the validation error. The return type would be ResponseEntity<Error>

使用这种方法,您只需要在一个地方为API中的所有端点实现控制器异常处理.这也使您的API易于在所有端点上具有统一的异常响应结构.这样可以简化客户端的异常处理.

With this approach, you only need to implement your controller exception handling in one place for all endpoints in your API. It also makes it easy for your API to have a uniform exception response structure across all endpoints. This simplifies exception handling for your clients.

这篇关于在Spring MVC或Spring-Boot中返回不同类型的ResponseEntity的最佳方法是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆