【发布时间】:2021-03-09 19:15:09
【问题描述】:
我在尝试将实体从包含且可选的 ArrayList 中拉出时遇到一些问题。当我做一个断点时,我得到代码下方的返回。我知道我很接近,但缺乏关于如何从返回给我的数据中提取 GrandClientDataCore@9463 的知识。
编辑在 for 循环之前添加上一行。
Error occured: java.util.Optional cannot be cast to net.glmhc.dmhwebservices.entities.GrandClientDataCores.
List<GrandClientDataCores> grandClientDataCoresList = getGrandClientDataCoreList(submitMode, grandClientDataCoreId);
for (GrandClientDataCores grandClientDataCores : grandClientDataCoresList) {
CDCPAErrors request = new CDCPAErrors();
request.setI(this.service.getRequestInfo(grandClientDataCores, submitMode, staff));
logToFile(outDir, String.format("req_%s.xml", new Object[] {grandClientDataCores}), request);
CDCPAErrorsResponse response = (CDCPAErrorsResponse)
getWebServiceTemplate().marshalSendAndReceive(getWebServiceUri(), request,
(WebServiceMessageCallback) new SoapActionCallback("http://tempuri.org/CDCPAErrors"));
logToFile(outDir, String.format("res_%s.xml", new Object[] {grandClientDataCoreId}), response);
DmhServicesCdcResponse responseObj = getResponse(submitMode, response);
this.service.saveResponse(grandClientDataCores, submitMode, responseObj, staff);
responses.add(responseObj);
}
这是 getGrandClientDataCoreList
protected List<GrandClientDataCores> getGrandClientDataCoreList(SubmitMode submitMode, String grandClientDataCore) throws Exception {
List<GrandClientDataCores> grandClientDataCoresList;
try {
grandClientDataCoresList = (List<GrandClientDataCores>) this.service.getGrandClientDataCoreList(submitMode, grandClientDataCore);
} catch ( Exception ex) {
throw new Exception(ex);
}
if (grandClientDataCore == null || grandClientDataCore.isEmpty()) {
throw new NoDataException("No CDC record to validate.");
}
return grandClientDataCoresList;
}
【问题讨论】:
-
列表
-
我的猜测是,这是因为它被 Spring Boot JPA 进一步包装,这使得任何 findbyid 都变成了 Optional。我将在我原来的问题中添加更多代码。
-
@Noobiedamus
this.service.getGrandClientDataCoreList的返回类型是什么? -
对象 getGrandClientDataCoreList (SubmitMode paraSubmitMode, String paramString) 抛出异常;来自服务接口的@ETO。
标签: java java-8 casting optional