这更像是泛型问题,以及如何扩展子类以使用定义的类型。
我有一个类APIResponse,我定义了通过泛型传递的数据类型,该类还存储其他详细信息,例如消息,我想传递给调用REST服务的UI的错误。
当我尝试使用指定的类型创建APIResponseEntity实例时,我得到一个错误。我可能在这里没有正确使用泛型,请告诉我如何将返回的数据类型传递给APIResponseEntity,最终可以在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);
}
当我尝试使用泛型创建APIResponseEntity对象时出现编译错误
@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;
}
答案 0 :(得分:1)
为什么使用new APIResponseEntity<>(HttpStatus.OK, banks)
代替new APIResponseEntity<>(HttpStatus.OK, response)
?您没有APIResponseEntity
构造函数,该构造函数将List<>
定义为第二个参数。
将您的代码更改为:
@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;
}