Marshall @XmlType annotated java object to a string (without @XmlRoot)

Search for a command to run...

No comments yet. Be the first to comment.
Prelude This is the story of how I fell into the rabbit hole of proxy automatic configuration (or PAC) files. My company lets me connect to their network by VPN. But we also use cloud services like GitHub. The enterprise GitHub is secured by an IP al...

I always found traceroute one of the most interesting network utilities, as it gives you an insight on how data travels through a network. If you ping a server and get a response, it’s easy to forget that the data travelled through a cascade of netwo...

The Certified Kubernetes Application Developer (https://training.linuxfoundation.org/certification/certified-kubernetes-application-developer-ckad/) is a great certification to train and prove your Kubernetes skills. For approximately USD 600 you can...

So you want to buy a PS5? A Playstation 5 is pretty hard to get at the moment. I really wanted one and asked in a couple of stores. They all told me that they have a huge waiting list and won't even add me to it. When searching the internet I found a...

It's called marshalling when you convert a xml object in memory to a serialized format like a string or file
It's called unmarshalling when you convert a xml file or string to an object in memory (for exmpale to a java class)
If the class you are trying to marshall has a @XmlRoot annotation it's pretty straight forward to marshall it.
val context: JAXBContext = JAXBContext.newInstance(Person::class.java)
val marshaller = context.createMarshaller()
// output pretty printed
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true)
val sw = StringWriter()
marshaller.marshal(person, sw)
println(sw.toString())
I ran into a case, where I wanted to test a function that has @XmlType annotated java class as an input parameter. The verify function of the Mockk library always returned false, because the nested java object doesn't implement an equals function. Since this java class should be easily serialized I tried to marshall it using JAXB. I got an error saying that the class doesn't have an @XMLRoot annotation, which indeed was true. Since the class is from a third party library I had to find a way to marshall it with only a @XmlType annotation. Turns out this can be done by wrapping the object in a JAXBElement with a QName.
val jaxbElement = JAXBElement<Person>(
QName("", "person"),
Person::class.java,
person
)
A full example would look like this. Let's say we have the following java class without a @XMLRoot annotation:
@XmlAccessorType(XMLAccessType.FIELD)
@XmlType(name = "person")
public class Person {
String firstName;
String lastName;
Person(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
}
Now we can marshall it using the following code:
val person = Person("firstName", "lastName")
val context: JAXBContext = JAXBContext.newInstance(Person::class.java)
val marshaller = context.createMarshaller()
val sw = StringWriter()
val jaxbElement = JAXBElement<Person>(QName("", "person"), Person::class.java, person)
marshaller.marshal(jaxbElement, sw)
println(sw.toString())