在 Spring MVC 或 Spring-Boot 中返回不同类型的 ResponseEntity 的最佳方法是什么

我使用 Spring MVC 4(或 Spring-Boot)编写了简单的 rest 应用程序。在控制器中,返回 ResponseEntity。但是在某些情况下,我想给出成功的 JSON,如果有验证错误,我想给出错误的 JSON。目前的成功和错误响应是完全不同的,所以我已经创建了两个类的错误和成功。在控制器中,如果内部逻辑正常,我想返回 ResponseEntity<Success>。否则我想返回 ResponseEntity<Error>。有没有什么办法。

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

235629 次浏览

I used to use a class like this. The statusCode is set when there is an error with the error message set in message. Data is stored either in the Map or in a List as and when appropriate.

/**
*
*/
package com.test.presentation.response;


import java.util.Collection;
import java.util.Map;


/**
* A simple POJO to send JSON response to ajax requests. This POJO enables  us to
* send messages and error codes with the actual objects in the application.
*
*
*/
@SuppressWarnings("rawtypes")
public class GenericResponse {


/**
* An array that contains the actual objects
*/
private Collection rows;


/**
* An Map that contains the actual objects
*/
private Map mapData;


/**
* A String containing error code. Set to 1 if there is an error
*/
private int statusCode = 0;


/**
* A String containing error message.
*/
private String message;


/**
* An array that contains the actual objects
*
* @return the rows
*/
public Collection getRows() {
return rows;
}


/**
* An array that contains the actual objects
*
* @param rows
*            the rows to set
*/
public void setRows(Collection rows) {
this.rows = rows;
}


/**
* An Map that contains the actual objects
*
* @return the mapData
*/
public Map getMapData() {
return mapData;
}


/**
* An Map that contains the actual objects
*
* @param mapData
*            the mapData to set
*/
public void setMapData(Map mapData) {
this.mapData = mapData;
}


/**
* A String containing error code.
*
* @return the errorCode
*/
public int getStatusCode() {
return statusCode;
}


/**
* A String containing error code.
*
* @param errorCode
*            the errorCode to set
*/
public void setStatusCode(int errorCode) {
this.statusCode = errorCode;
}


/**
* A String containing error message.
*
* @return the errorMessage
*/
public String getMessage() {
return message;
}


/**
* A String containing error message.
*
* @param errorMessage
*            the errorMessage to set
*/
public void setMessage(String errorMessage) {
this.message = errorMessage;
}

}

Hope this helps.

i am not sure but, I think you can use @ResponseEntity and @ResponseBody and send 2 different one is Success and second is error message like :

@RequestMapping(value ="/book2", produces =MediaType.APPLICATION_JSON_VALUE )
@ResponseBody
Book bookInfo2() {
Book book = new Book();
book.setBookName("Ramcharitmanas");
book.setWriter("TulasiDas");
return book;
}


@RequestMapping(value ="/book3", produces =MediaType.APPLICATION_JSON_VALUE )
public ResponseEntity<Book> bookInfo3() {
Book book = new Book();
book.setBookName("Ramayan");
book.setWriter("Valmiki");
return ResponseEntity.accepted().body(book);
}

For more detail refer to this: http://www.concretepage.com/spring-4/spring-4-mvc-jsonp-example-with-rest-responsebody-responseentity

You can return generic wildcard <?> to return Success and Error on a same request mapping method

public ResponseEntity<?> method() {
boolean b = // some logic
if (b)
return new ResponseEntity<Success>(HttpStatus.OK);
else
return new ResponseEntity<Error>(HttpStatus.CONFLICT); //appropriate error code
}

@Mark Norman answer is the correct approach

Its possible to return ResponseEntity without using generics, such as follows,

public ResponseEntity method() {
boolean isValid = // some logic
if (isValid){
return new ResponseEntity(new Success(), HttpStatus.OK);
}
else{
return new ResponseEntity(new Error(), HttpStatus.BAD_REQUEST);
}
}

Here is a way that I would do it:

public ResponseEntity < ? extends BaseResponse > message(@PathVariable String player) { //REST Endpoint.


try {
Integer.parseInt(player);
return new ResponseEntity < ErrorResponse > (new ErrorResponse("111", "player is not found"), HttpStatus.BAD_REQUEST);
} catch (Exception e) {




}
Message msg = new Message(player, "Hello " + player);
return new ResponseEntity < Message > (msg, HttpStatus.OK);


}


@RequestMapping(value = "/getAll/{player}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity < List < ? extends BaseResponse >> messageAll(@PathVariable String player) { //REST Endpoint.


try {
Integer.parseInt(player);
List < ErrorResponse > errs = new ArrayList < ErrorResponse > ();
errs.add(new ErrorResponse("111", "player is not found"));
return new ResponseEntity < List < ? extends BaseResponse >> (errs, HttpStatus.BAD_REQUEST);
} catch (Exception e) {




}
Message msg = new Message(player, "Hello " + player);
List < Message > msgList = new ArrayList < Message > ();
msgList.add(msg);
return new ResponseEntity < List < ? extends BaseResponse >> (msgList, HttpStatus.OK);


}

You can also implement like this to return Success and Error on a same request mapping method,use Object class(Parent class of every class in java) :-

public ResponseEntity< Object> method() {
boolean b = //  logic  here
if (b)
return new ResponseEntity< Object>(HttpStatus.OK);
else
return new ResponseEntity< Object>(HttpStatus.CONFLICT); //appropriate error code
}

You can use a map with your object or string like bellow :

@RequestMapping(value = "/path",
method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public ResponseEntity<Map<String,String>> getData(){


Map<String,String> response = new HashMap<String, String>();


boolean isValid = // some logic
if (isValid){
response.put("ok", "success saving data");
return ResponseEntity.accepted().body(response);
}
else{
response.put("error", "an error expected on processing file");
return ResponseEntity.badRequest().body(response);
}


}

I recommend using Spring's @ControllerAdvice to handle 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:

  • 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>

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.

Note: if you upgrade from spring boot 1 to spring boot 2 there is a ResponseStatusException which has a Http error code and a description.

So, you can effectively use generics they way it is intended.

The only case which is a bit challenging for me, is the response type for a status 204 (ok with no body). I tend to mark those methods as ResponseEntity<?>, because ResponseEntity<Void> is less predictive.

Spring 2 introduced ResponseStatusException using this you can return String, different HTTP status code, DTO at the same time.

@PostMapping("/save")
public ResponseEntity<UserDto> saveUser(@RequestBody UserDto userDto) {
if(userDto.getId() != null) {
throw new ResponseStatusException(HttpStatus.NOT_ACCEPTABLE,"A new user cannot already have an ID");
}
return ResponseEntity.ok(userService.saveUser(userDto));
}

Using custom exception class you can return different HTTP status code and dto objects.

@PostMapping("/save")
public ResponseEntity<UserDto> saveUser(@RequestBody UserDto userDto) {
if(userDto.getId() != null) {
throw new UserNotFoundException("A new user cannot already have an ID");
}
return ResponseEntity.ok(userService.saveUser(userDto));
}

Exception class

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;


@ResponseStatus(value = HttpStatus.NOT_FOUND, reason = "user not found")
public class UserNotFoundException extends RuntimeException {


public UserNotFoundException(String message) {


super(message);
}
}

For exceptional cases, I will recommend you to adopt RFC-7807 Problem Details for HTTP APIs standard in your application.

Zalando's Problems for Spring provides a good integration with Spring Boot, you can integrate it easily with your existing Spring Boot based application. Just like what JHipster did.

After adopting RFC-7087 in your application, just throw Exception in your controller method, and you will get a detailed and standard error response like:

   {
"type": "https://example.com/probs/validation-error",
"title": "Request parameter is malformed.",
"status": 400
"detail": "Validation error, value of xxx should be a positive number.",
"instance": "/account/12345/msgs/abc",
}

To follow up the answer of @MarkNorman, I would say you have to define the mapping between the exceptions coming from your service to your controller (HTTP Error Code).

  • Success response are mapped to 200 status (OK)
  • Validation exception mapped with 400 status (BAD_REQUEST)
  • Record not found mapped with 404 status (NOT_FOUND)
  • Other exception mapped to 500 status (INTERNAL_SERVER_ERROR)

For example, your code should look something like this:

@GetMapping("/persons/{id}")
public ResponseEntity<Success> findPersonById(@PathVariable("id") Long id) {
try {
var person = service.findById(id);
var message = new Message(HttpStatus.OK, getCurrentDateTime(), person);
return message;
} catch(ServiceException exception) {
throw new NotFoundException("An error occurs while finding a person", exception);
}
}

All the exceptions thrown should be redirect in the ControllerAdvice

@ExceptionHandler(NotFoundException.class)
public ResponseEntity<Error> handleNotFoundException(NotFoundException exception) {
var error = new Error(HttpStatus.NOT_FOUND,
getCurrentDateTime(),
exception.getMessage());


return ResponseEntity.status(HttpStatus.NOT_FOUND).body(error);
}

The main thing to understand is that your RestControllers only understand HTTP protocol and HTTP Code for responses.

You can use @ExceptionHandler Controller Advice to return custom Return Object in case error. See the example code below which returns 400 with Custom response in case of Validation Error.

@ControllerAdvice
public class RestExceptionHandler {
@ExceptionHandler(value = InputValidationError.class)
public ResponseEntity<ValidationErrorResponse> handleException(InputValidationError ex){
return ResponseEntity.badRequest().body(ex.validationErrorResponse);
}
}