Spring MVC: How to return different type in ResponseEntity body Spring MVC: How to return different type in ResponseEntity body spring spring

Spring MVC: How to return different type in ResponseEntity body


There are two problems here:-

  • Your return type has to be changed to match the two response subclasses ResponseEntity<? extends AbstractResponse>

  • When you instantiate your ResponseEntity you cannot use the simplified <> syntax you have to specify which response class you are going to use new ResponseEntity<ErrorResponse> (errResponse, HTTPStatus.BAD_REQUEST);

     @ResponseBody ResponseEntity<? extends AbstractResponse> createUser(@RequestBody String requestBody) {     if(!valid(requestBody) {         ErrorResponse errResponse = new ErrorResponse();         //populate with error information         return new ResponseEntity<ErrorResponse> (errResponse, HTTPStatus.BAD_REQUEST);     }     createUser();     CreateUserSuccessResponse successResponse = new CreateUserSuccessResponse();     // populate with more info     return new ResponseEntity<CreateUserSuccessResponse> (successResponse, HTTPStatus.OK); }


Another approach would be using error handlers

@ResponseBody ResponseEntity<CreateUserSuccessResponse> createUser(@RequestBody String requestBody) throws UserCreationException {    if(!valid(requestBody) {        throw new UserCreationException(/* ... */)    }    createUser();    CreateUserSuccessResponse successResponse = new CreateUserSuccessResponse();    // populate with more info    return new ResponseEntity<CreateUserSuccessResponse> (successResponse, HTTPSatus.OK);}public static class UserCreationException extends Exception {    // define error information here}@ExceptionHandler(UserCreationException.class)@ResponseStatus(HttpStatus.BAD_REQUEST)@ResponseBodypublic ErrorResponse handle(UserCreationException e) {    ErrorResponse errResponse = new ErrorResponse();    //populate with error information from the exception    return errResponse;}

This approach enables the possibility of returning any kind of object, so an abstract super class for the success case and the error case (or even cases) is no longer necessary.