How to Specify XML Element for Java Record classes in java17

139 Views Asked by At

I java 17 a Java Record class, how can i customize the XML Serialization attributes like @XmlElement @XmlRootElement

public record Person(String name, int age) {
    // Some record body
}
1

There are 1 best solutions below

0
On BEST ANSWER

You can specify the class level annotations at Record level, and field level at Constructor arg level

import jakarta.xml.bind.annotation.XmlElement;
import jakarta.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public record Person(@XmlElement String name, @XmlElement int age) {
    // body
}