来自可选对象的实体 (Java 8)

Entity from Optional Object (Java 8)

我在尝试从包含可选的 ArrayList 中拉出实体时遇到一些问题。当我做一个断点时,我在代码下面得到 return 。我知道我很接近,但缺乏关于如何从 returned 给我的数据中提取 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;
    }

您必须对可选对象调用 get() 才能检索其值。您不能只将 Optional<T> 强制转换为其他内容。根据调试图像,grandClientDataCoresList 的声明如下所示:

List<Optional<GrandClientDataCores>> grandClientDataCoresList ...

因此你需要这样的东西:

for (Optional<GrandClientDataCores> gcdcOpt: grandClientDataCoresList) {
    GrandClientDataCores gcdc = gcdcOpt.get();
    ....

grandClientDataCores 中的值属于 Optional<GrandClientDataCores>.

类型

你的实际错误在这里:

   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;
    }

您会发现 this.service.getGrandClientDataCoreList 返回的实际类型是 List<Optional<GrandClientDataCores>> 因此您必须在许多地方相应地更新您的代码。对于初学者...

   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;
    }

以及您调用此方法的任何地方。

使用 Optional 的“get()”函数,它将检索对象本身。 请注意,如果没有对象填充到此 Optional 中,它将抛出异常。

List 中的 GrandClientDataCores 值用 Optional 包裹,因此您必须检查值是否存在:

grandClientDataCores.isPresent()

如果是,那就得到它:

grandClientDataCores.get();

或者,您也可以这样做:

grandClientDataCores.orElse(new GrandClientDataCores())

我推荐阅读 this