Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring MVC: How to return different type in ResponseEntity body

In my request handler I want to do some validation and based on the outcome of validation check I will return different response (success/error). So I create a abstract class for the response object and make 2 subclasses for failure case and successful case. The code looks something like this, but it doesn't compile, complaining that errorResponse and successResponse cannot be converted to AbstractResponse.

I'm quite new to Java Generic and Spring MVC so I don't know of a simple way to solve this.

@ResponseBody ResponseEntity<AbstractResponse> createUser(@RequestBody String requestBody) {
    if(!valid(requestBody) {
        ErrorResponse errResponse = new ErrorResponse();
        //populate with error information
        return new ResponseEntity<> (errResponse, HTTPStatus.BAD_REQUEST);
    }
    createUser();
    CreateUserSuccessResponse successResponse = new CreateUserSuccessResponse();
    // populate with more info
    return new ResponseEntity<> (successResponse, HTTPSatus.OK);
}
like image 650
dnang Avatar asked Oct 31 '13 15:10

dnang


1 Answers

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);
     }
    
like image 137
gadget Avatar answered Oct 26 '22 11:10

gadget