如何从另一个 class 访问用户定义的 List<> 值?

How to access user defined List<> values from another class?

我正在用 spring-boot.Basically 开发一个 API 我想从服务 class 的 PortedNumber class 获取所有值并存储它们到文本文件。

public class Service {
    PortedNumber portedNumber = new PortedNumber();
    public AcknowledgeMessage checkRequestValidity(Request request) throws IOException {
        AcknowledgeMessage acknowledgeMessage=new AcknowledgeMessage();
        long msisdn = portedNumber.getNumber();
        String recipientRC = portedNumber.getRecipientRC();
        String donorRC = portedNumber.getDonorRC();
        String nrhRC = portedNumber.getNrhRC();
        
        try(FileWriter fw = new FileWriter("myfile.txt", true);
                BufferedWriter bw = new BufferedWriter(fw);
                PrintWriter out = new PrintWriter(bw))
            {
                out.println(msisdn+"\t"+recipientRC+"\t"+donorRC+"\t"+nrhRC);   
            } catch (IOException e) {
                //exception handling left as an exercise for the reader
            }

        return acknowledgeMessage;
    }
}
public class Request {

    protected List<PortedNumber> singleNumber;

    public List<PortedNumber> getSingleNumber() {
        if (singleNumber == null) {
            singleNumber = new ArrayList<PortedNumber>();
        }
        return this.singleNumber;
    }
}



public class PortedNumber {

    protected long number;
    @XmlElement(required = true)
    protected String recipientRC;
    @XmlElement(required = true)
    protected String donorRC;
    @XmlElement(required = true)
    protected String nrhRC;
    @XmlElement(required = true)
    protected PortedAction portedAction;
    protected List<ExtensionElement> extension;

    public long getNumber() {
        return number;
    }

    public void setNumber(long value) {
        this.number = value;
    }

    public String getRecipientRC() {
        return recipientRC;
    }

    public void setRecipientRC(String value) {
        this.recipientRC = value;
    }

    public String getDonorRC() {
        return donorRC;
    }
   

    public void setDonorRC(String value) {
        this.donorRC = value;
    }

    public String getNrhRC() {
        return nrhRC;
    }

    public void setNrhRC(String value) {
        this.nrhRC = value;
    }

    public PortedAction getPortedAction() {
        return portedAction;
    }

    public void setPortedAction(PortedAction value) {
        this.portedAction = value;
    } 
}

如何从 Service class 中获取 PortedNumbergetNumber() 的值?

我试过 long number = portedNumber.getNumber(); 但是,它行不通。它为 msisdn 存储 0,为其他存储 null。

我无法获取 Arraylist 值。我想获取 Arraylist 的所有值并将其存储到文本文件中。

您使用 PortedNumber portedNumber = new PortedNumber();Service 中实例化了 portedNumber 属性。这意味着它没有初始化属性,并且所有属性都将具有它们的默认值(null 对象,0 长)。

您从未设置这些属性,因此预计 It stores 0 for msisdn and null for others. 如您所写。


如果我正确理解了你的问题,你想遍历 request 中的 PortedNumber 列表并将它们写入文件。如果是这种情况,请尝试以下操作:

public class Service {

    public AcknowledgeMessage checkRequestValidity(NPCHRequest request) throws IOException {
        AcknowledgeMessage acknowledgeMessage=new AcknowledgeMessage();
        
        try(FileWriter fw = new FileWriter("myfile.txt", true);
                BufferedWriter bw = new BufferedWriter(fw);
                PrintWriter out = new PrintWriter(bw)) {
                for (PortedNumber portedNumber : request.getSingleNumber()) {
                    long msisdn = portedNumber.getNumber();
                    String recipientRC = portedNumber.getRecipientRC();
                    String donorRC = portedNumber.getDonorRC();
                    String nrhRC = portedNumber.getNrhRC();
                    out.println(msisdn+"\t"+recipientRC+"\t"+donorRC+"\t"+nrhRC); 
                }  
            } catch (IOException e) {
                //exception handling left as an exercise for the reader
            }

        return acknowledgeMessage;
    }
}