簡體   English   中英

數組列表<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 個空白數組“[]”? 我犯了什么愚蠢的錯誤嗎?

如果我很好地理解你的代碼,你想從給定目錄中的許多文件中讀取數據並將其放入 Array.

在讀取所有文件后,您應該使用ObjectMapper 所以把它放在result.forEach((n) -> { .. }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM