简体   繁体   中英

JAXB - change property name without changing variable name in class

So I have code like this:

@XmlRootElement(name = "person")
@XmlType(propOrder = {"name", "secondName"})
public class Person {
   private String name;
   private String secondName;

   public void setName(String name) {
      this.name = name;
   }

   public String getName() {
      return name;
   }

   public void setSecondName(String secondName) {
      this.secondName = secondName;
   }

   public String getSecondName() {
      return secondName;
   }
}

And when I want to create XML file it makes me:

<person>
   <name>John</name>
   <secondName>Smith</secondName>
</person>

Is it any way to make in xml file <second-name> instead of <secondName> without changing in class on private String second-name ?

Problem solved. I should just do this:

@XmlElement(name="second-name")
public String getSecondName() {
   return secondName;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM