[英]Entity from Optional Object (Java 8)
I am having some issue while trying to pull an entity out of an ArrayList that holds and Optional.我在尝试将实体从持有且可选的 ArrayList 中拉出时遇到一些问题。 When I do a breakpoint I get the return below the code.
当我做一个断点时,我得到代码下方的返回。 I know I am close but lack the knowledge on how to pull the GrandClientDataCore@9463 out of the data being returned to me.
我知道我很接近,但缺乏关于如何将 GrandClientDataCore@9463 从返回给我的数据中提取出来的知识。
Edited to add the previous line before the for loop.编辑以在 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);
}
This is the getGrandClientDataCoreList这是 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;
}
You have to invoke get()
on the optional to retrieve its value.您必须在可选项上调用
get()
以检索其值。 You cannot just cast Optional<T>
to something else.您不能只将
Optional<T>
转换为其他东西。 According to the debug image, the declaration of grandClientDataCoresList
looked like this:根据调试图像,
grandClientDataCoresList
的声明如下所示:
List<Optional<GrandClientDataCores>> grandClientDataCoresList ...
Therefore you need something like this:因此,您需要这样的东西:
for (Optional<GrandClientDataCores> gcdcOpt: grandClientDataCoresList) {
GrandClientDataCores gcdc = gcdcOpt.get();
....
values in grandClientDataCores
are of type Optional<GrandClientDataCores>
. grandClientDataCores
中的值属于Optional<GrandClientDataCores>
类型。
Your actual error is here:您的实际错误在这里:
protected List<GrandClientDataCores> getGrandClientDataCoreList(SubmitMode submitMode, String grandClientDataCore) throws Exception {
List<GrandClientDataCores> grandClientDataCoresList;
try {
grandClientDataCoresList = (List<GrandClientDataCores>) this.service.getGrandClientDataCoreList(submitMode, grandClientDataCore);
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
This cast is invalid
} catch ( Exception ex) {
throw new Exception(ex);
}
if (grandClientDataCore == null || grandClientDataCore.isEmpty()) {
throw new NoDataException("No CDC record to validate.");
}
return grandClientDataCoresList;
}
You will find that the actual type returned by this.service.getGrandClientDataCoreList
is List<Optional<GrandClientDataCores>>
so you must update your code accordingly, in a number of places.你会发现
this.service.getGrandClientDataCoreList
返回的实际类型是List<Optional<GrandClientDataCores>>
所以你必须在很多地方相应地更新你的代码。 For starters...对于初学者...
protected List<Optional<GrandClientDataCores>> getGrandClientDataCoreList(SubmitMode submitMode, String grandClientDataCore) throws Exception {
List<Optional<GrandClientDataCores>> grandClientDataCoresList;
try {
grandClientDataCoresList = 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;
}
and everywhere that you invoke this method.以及您调用此方法的任何地方。
GrandClientDataCores values in List are wrapped with Optional, so you have to check if value present: List 中的 GrandClientDataCores 值使用 Optional 包装,因此您必须检查值是否存在:
grandClientDataCores.isPresent()
and if it is then just get it:如果是那么就得到它:
grandClientDataCores.get();
Alternatively, you can do something like that:或者,您可以执行以下操作:
grandClientDataCores.orElse(new GrandClientDataCores())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.