Kar Kar - 3 months ago 7
reST (reStructuredText) Question

Passing generic type from one class to another?

This is more a question of generics and how to extend the child class to use a type defined.

I have a class APIResponse to which I define the type of data passed via generics, this class also stores other details such as messages, errors that I want to pass to the UI invoking the REST service.

I get a errror when I try to create an instance of APIResponseEntity with the type specified. I might not be using generics correctly here, please can you tell me how to pass the type of data returned into APIResponseEntity that can ultimately be used in APIResponse.

public class APIResponseEntity<T> extends ResponseEntity<APIResponse<T>> {


public APIResponseEntity(HttpStatus pStatusCode) {
super(pStatusCode);
}

public APIResponseEntity(HttpStatus pStatusCode, MultiValueMap<String, String> pHeaders) {
super(pHeaders, pStatusCode);
}

public APIResponseEntity(HttpStatus pStatusCode, APIResponse<T> pBody) {
super(pBody, pStatusCode);
}

public APIResponseEntity(HttpStatus pStatusCode, APIResponse<T> pBody, MultiValueMap<String, String> pHeaders) {
super(pBody, pHeaders, pStatusCode);
}


}

@JsonPropertyOrder({ "status", "error", "data", "messages" })
public final class APIResponse<T> {


private APIResponseStatus _status;

private T _data;

private APIError _error;

private List<APIMessage> _messages;


public APIResponse() {
super();
_messages = new ArrayList<>();
}

public APIResponse(T pData) {
this();
_status = APIResponseStatus.OK;
_data = pData;
}

public APIResponse(APIError pError) {
this();
_status = APIResponseStatus.ERROR;
_error = pError;
}


public APIResponseStatus getStatus() {
return _status;
}

public void setStatus(APIResponseStatus pStatus) {
_status = pStatus;
}

public Object getData() {
return _data;
}

public void setData(T pData) {
_data = pData;
}

public APIError getError() {
return _error;
}

public void setError(APIError pError) {
_error = pError;
}

public List<APIMessage> getMessages() {
return _messages;
}

public void setMessages(List<APIMessage> pMessages) {
_messages = pMessages;
}

private void addMessage(APIMessageStatus pMessageStatus, String pMessage) {
_messages.add(new APIMessage(pMessageStatus, pMessage));
}

public void addMessageInfo(String pMessage) {
addMessage(APIMessageStatus.INFO, pMessage);
}

public void addMessageWarning(String pMessage) {
addMessage(APIMessageStatus.WARNING, pMessage);
}

public void addMessageError(String pMessage) {
addMessage(APIMessageStatus.ERROR, pMessage);
}


I am getting a compilation error when I try to create a APIResponseEntity object with generics

@RequestMapping(value = "/banks", method = RequestMethod.GET)
public APIResponseEntity<List<Bank>> getBanks(WebRequest pWebRequest) {

IUser user = getUser(pWebRequest);
BankCriteria criteria = new BankCriteria();
List<Bank> banks= _bankService.getBanks(user, criteria);
APIResponse<List<Bank>> response = new APIResponse<>(banks);
APIResponseEntity<List<Bank>> entity = new APIResponseEntity<>(HttpStatus.OK, banks); // errror , cannot infer type arguments for APIResponseEntity
return entity;

}

Answer

Why are you using new APIResponseEntity<>(HttpStatus.OK, banks) instead of new APIResponseEntity<>(HttpStatus.OK, response)? You do not have an APIResponseEntity constructor which takes a List<> as second argument defined.

Change your code to this:

@RequestMapping(value = "/banks", method = RequestMethod.GET)
public APIResponseEntity<List<Bank>> getBanks(WebRequest pWebRequest) {

    IUser user = getUser(pWebRequest);
    BankCriteria criteria = new BankCriteria();
    List<Bank> banks= _bankService.getBanks(user, criteria);
    APIResponse<List<Bank>> response = new APIResponse<>(banks);
    APIResponseEntity<List<Bank>> entity =  new APIResponseEntity<List<Bank>>(HttpStatus.OK, response); // errror , cannot infer type arguments for APIResponseEntity
    return entity;
}
Comments