Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/367.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将POJO打印到System.out问题_Java_Jaxb - Fatal编程技术网

Java 将POJO打印到System.out问题

Java 将POJO打印到System.out问题,java,jaxb,Java,Jaxb,在一些教程()之后,我在控制台上没有得到相同的输出。 本教程介绍如何使用JAXB API将Java对象转换为XML或从XML转换为Java对象-JAXBContext、Unmarshaller、Marshaller 这是POJO代码: package com.jaxb.example; import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.XmlElement; import javax.

在一些教程()之后,我在控制台上没有得到相同的输出。 本教程介绍如何使用JAXB API将Java对象转换为XML或从XML转换为Java对象-JAXBContext、Unmarshaller、Marshaller

这是POJO代码:

package com.jaxb.example;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Customer {

    private String name;
    private int age;
    private int id;

    public String getName() {
        return name;
    }
    @XmlElement
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    @XmlElement
    public void setAge(int age) {
        this.age = age;
    }
    public int getId() {
        return id;
    }
    @XmlAttribute
    public void setId(int id) {
        this.id = id;
    }

}
这是解组代码:

package com.jaxb.example;

import java.io.File;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

public class JAXBExampleTestUnmarshall {

    public static void main(String [] args){

        try {
            File file = new File("./jaxb-data/file.xml");

            JAXBContext context = JAXBContext.newInstance(Customer.class);
            Unmarshaller jaxbUnmarshaller = context.createUnmarshaller();

            Customer customer = (Customer)jaxbUnmarshaller.unmarshal(file);
            //System.out.println(customer.getId());
            //System.out.println(customer.getName());
            //System.out.println(customer.getAge());
            System.out.println(customer);

        } catch (JAXBException e) {
            // TODO: handle exception
            e.printStackTrace();
        }
    }

}
这是
/jaxb data/file.xml
文件内容:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<customer id="1">
    <age>33</age>
    <name>Some Name</name>
</customer>

33
某个名字
我得到了
com.jaxb.example。Customer@15e8d410
当我运行这个类时


问题:为什么我在输出时没有得到
Customer[name=Some name,age=33,id=1]

您必须覆盖
Customer#toString
,当您系统输出一个对象时,它会被隐式调用。标准实现是这个对象的完全限定类名和hexcoded-
hashcode()

您必须覆盖
Customer\toString
,当您系统输出一个对象时,它会被隐式调用。标准实现是此对象的完全限定类名和hexcoded-
hashcode()

您需要重写
toString()
方法来有意义地表示对象
System.out.println()
调用
toString()
方法,您可以看到从
toString()
方法返回的字符串

public String toString(){
   return "Customer [name =" + name+ ", age=" + age
            + ",id =" + id "]";
 }

您需要重写
toString()
方法,以便对对象进行有意义的表示
System.out.println()
调用
toString()
方法,您可以看到从
toString()
方法返回的字符串

public String toString(){
   return "Customer [name =" + name+ ", age=" + age
            + ",id =" + id "]";
 }
我明白了,
toString()
Customer
类的教程中没有实现,所以我假设它应该是默认行为。显然不是。谢谢。我知道,
toString()
Customer
类的教程中没有实现,所以我假设它应该是默认行为。显然不是。谢谢