ArrayList<pojo> 在使用 Jackson Object Mapper 打印时在列表中创建空数组

ArrayList<pojo> is creating null arrays in a list when printed with Jackson Object Mapper

我正在尝试读取一个带分隔符的文件,将其映射到 pojo,创建该 POJO 对象的数组列表并打印它。但是看起来 ArrayList 在使用 Jackson 2 Object Mapper 打印时正在列表中创建空数组。

  1. 请查找代码片段:
package ftp.service;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import javax.jws.WebMethod;
import javax.jws.WebService;
import com.adac.ftp.model.Tax;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;


@WebService
public class FTPDataExtractService {

    @WebMethod
    public void ftpDataExtractMethod() throws IOException {

        File source = new File("D:/RAW_DATA.zip");
        String out = "D:/Unzipped";

        unzip(source, out);

        try (Stream<Path> walk = Files.walk(Paths.get("D:/Unzipped"))) {

            List<String> result = walk.filter(Files::isRegularFile).map(x -> x.toString()).collect(Collectors.toList());

            System.out.println(result);
            List<Tax> taxList = new ArrayList<Tax>();



            result.forEach((n) -> {

                String path = n;

                try { 

                    String line = "";
                    FileInputStream file = new FileInputStream(path);
                    BufferedReader reader = new BufferedReader(new InputStreamReader(file));




                    while ((line = reader.readLine()) != null) {
                        String[] row = line.split("\|");



                             if (path.contains("TRANSACTION_FLIGHT")) {

                                 Tax tax = new Tax(row[0], row[1], row[2], row[3], row[4], row[5], row[6]);

                                   taxList.add(tax); 


                             }

                        }
                    ObjectMapper req_mapper = new ObjectMapper();
                    req_mapper.enable(SerializationFeature.INDENT_OUTPUT);
                    String json_req = req_mapper.writeValueAsString(taxList);   
                    System.out.println(json_req); 

                    reader.close();

                } catch (FileNotFoundException ex) {
                    Logger.getLogger(Collection.class.getName()).log(Level.SEVERE, null, ex);
                } catch (IOException ex) {
                    Logger.getLogger(Collection.class.getName()).log(Level.SEVERE, null, ex);
                }

            });
        }

    }



    public static void unzip(File source, String out) throws IOException {
        try (ZipInputStream zis = new ZipInputStream(new FileInputStream(source))) {

            ZipEntry entry = zis.getNextEntry();

            while (entry != null) {

                File file = new File(out, entry.getName());

                if (entry.isDirectory()) {
                    file.mkdirs();
                } else {
                    File parent = file.getParentFile();

                    if (!parent.exists()) {
                        parent.mkdirs();
                    }

                    try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file))) {

                        byte[] buffer = new byte[Math.toIntExact(entry.getSize())];

                        int location;

                        while ((location = zis.read(buffer)) != -1) {
                            bos.write(buffer, 0, location);
                        }
                    }
                }
                entry = zis.getNextEntry();
            }
        }
    }
}
  1. 正在读取的文件如下:
10797|SRF002|AD4|SIAD104120619017|WY 0634|MCT|2019-06-11|||Y|A
10797|SRF003|AD8|SIAD308110619028|EY 0278|MLE|2019-06-10|||J|A
10797|SRF002|AD5|SIAD105120619013|EY 0226|AMD|2019-06-11|||Y|A
10797|SRF003|AD8|SIAD308110619020|EY 0101|JFK|2019-06-10|||J|A
10797|SRF002|AD4|SIAD104110619015|ME 0419|BEY|2019-06-10|||Y|A
10797|SRF002|AD4|SIAD104120619007|MS 0917|CAI|2019-06-11|||Y|A
10797|SRF003|AD8|SIAD308120619059|EY 0333|JED|2019-06-11|||Y|A
10797|SRF003|AD8|SIAD308110619027|EY 0472|CGK|2019-06-10|||J|A
10797|SRF003|AD8|SIAD308120619063|EY 0025|LHR|2019-06-11|||Y|A
10797|SRF003|AD8|SIAD308100619067|EY 0315|RUH|2019-06-10|||J|A
10797|SRF003|AD8|SIAD308120619058|EY 0653|CAI|2019-06-11|||Y|A
10797|SRF002|AD5|SIAD105110619027|6E 1834|BOM|2019-06-10|||Y|A
10797|SRF003|AD8|SIAD308110619046|EY 0282|COK|2019-06-10|||Y|A
10797|SRF002|AD5|SIAD105110619031|EY 0424|MNL|2019-06-11|||Y|A
10797|SRF003|AD8|SIAD308110619061|EY 0005|MUC|2019-06-11|||Y|A
10797|SRF002|AD4|SIAD104100619024|EY 0233|ISB|2019-06-09|||Y|A
10797|SRF003|AD8|SIAD308120619030|EY 0019|LHR|2019-06-11|||Y|A
10797|SRF002|AD4|SIAD104100619021|EY 0653|CAI|2019-06-09|||Y|A
10797|SRF002|AD5|SIAD105100619011|EY 0317|RUH|2019-06-09|||Y|A
10797|SRF002|AD4|SIAD104120619020|RJ 0621|AMM|2019-06-11|||Y|A
  1. 控制台打印如下:
[ ]
[ ]
[ ]
[ ]
[ {
  "taxid" : "10797",
  "taxamount" : "SRF002",
  "currency" : "AD4",
  "taxbaseamount" : "SIAD104120619017",
  "taxexcgrate" : "WY 0634",
  "taxexcgdate" : "MCT",
  "taxexcgtype" : "2019-06-11"
}, {
  "taxid" : "10797",
  "taxamount" : "SRF003",
  "currency" : "AD8",
  "taxbaseamount" : "SIAD308110619028",
  "taxexcgrate" : "EY 0278",
  "taxexcgdate" : "MLE",
  "taxexcgtype" : "2019-06-10"
}, {
  "taxid" : "10797",
  "taxamount" : "SRF002",
  "currency" : "AD5",
  "taxbaseamount" : "SIAD105120619013",
  "taxexcgrate" : "EY 0226",
  "taxexcgdate" : "AMD",
  "taxexcgtype" : "2019-06-11"
}, {
  "taxid" : "10797",
  "taxamount" : "SRF003",
  "currency" : "AD8",
  "taxbaseamount" : "SIAD308110619020",
  "taxexcgrate" : "EY 0101",
  "taxexcgdate" : "JFK",
  "taxexcgtype" : "2019-06-10"
}, {
  "taxid" : "10797",
  "taxamount" : "SRF002",
  "currency" : "AD4",
  "taxbaseamount" : "SIAD104110619015",
  "taxexcgrate" : "ME 0419",
  "taxexcgdate" : "BEY",
  "taxexcgtype" : "2019-06-10"
}, {
  "taxid" : "10797",
  "taxamount" : "SRF002",
  "currency" : "AD4",
  "taxbaseamount" : "SIAD104120619007",
  "taxexcgrate" : "MS 0917",
  "taxexcgdate" : "CAI",
  "taxexcgtype" : "2019-06-11"
}, {
  "taxid" : "10797",
  "taxamount" : "SRF003",
  "currency" : "AD8",
  "taxbaseamount" : "SIAD308120619059",
  "taxexcgrate" : "EY 0333",
  "taxexcgdate" : "JED",
  "taxexcgtype" : "2019-06-11"
}, {
  "taxid" : "10797",
  "taxamount" : "SRF003",
  "currency" : "AD8",
  "taxbaseamount" : "SIAD308110619027",
  "taxexcgrate" : "EY 0472",
  "taxexcgdate" : "CGK",
  "taxexcgtype" : "2019-06-10"
}
------------------<more data here as per the delimited file>-----
]

我不明白为什么要在开头打印 4 个空白数组“[]”?我是不是犯了什么愚蠢的错误?

如果我很理解你的代码,你想从给定目录中的许多文件中读取数据并将其放入数组中。

您应该在读取所有文件后使用ObjectMapper。所以放在result.forEach((n) -> { .. }

之后