gpt4 book ai didi

java - 将泛型类型从一个类传递到另一个类?

转载 作者:行者123 更新时间:2023-11-30 07:10:38 25 4
gpt4 key购买 nike

这更多的是泛型以及如何扩展子类以使用定义的类型的问题。

我有一个 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;

}

最佳答案

你为什么使用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;
}

关于java - 将泛型类型从一个类传递到另一个类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39288983/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com