Reputation: 2347
If i declare the namespace on the root element, like this:
@JacksonXmlRootElement(namespace = "urn:stackify:jacksonxml", localName = "PersonData")
public class Person {
private String id;
private String name;
private String note;
}
It produces:
<PersonData xmlns="urn:stackify:jacksonxml">
<id xmlns="">12345</id>
<name xmlns="">Graham</name>
<note xmlns="">Hello</note>
</PersonData>
But I want the namespace only on the root element. The xmlns attribute should not appear on child elements.
How can i archive this?
Upvotes: 12
Views: 13074
Reputation: 11
Also works well with immutables library and json annotations (if you need to serialize/deserialize both in JSON and in XML)
@Value.Immutable
@JsonRootName(value = "PersonData", namespace = "urn:stackify:jacksonxml")
public interface Person extends Serializable {
}
Upvotes: 1
Reputation: 1593
There is a workaround which I found more elegant for me.
You may define constant for your namespace like this:
@JacksonXmlRootElement(localName = "PersonData")
public class Person {
@JacksonXmlProperty(isAttribute = true)
private final String xmlns = "urn:stackify:jacksonxml";
private String id;
private String name;
private String note;
}
Upvotes: 12
Reputation: 2679
You need to specify the same namespace as the root element in each attribute:
@JacksonXmlRootElement(namespace = "urn:stackify:jacksonxml", localName = "PersonData")
public class Person {
@JacksonXmlProperty(namespace = "urn:stackify:jacksonxml")
private String id;
@JacksonXmlProperty(namespace = "urn:stackify:jacksonxml")
private String name;
@JacksonXmlProperty(namespace = "urn:stackify:jacksonxml")
private String note;
}
Its a bit tiresome, but its the only way I found to avoid the unnecessary namespaces.
Upvotes: 8