Unnecessary Namespace in Jackson XML 2.6.1 + Woodstox 4.4.1
Asked Answered
P

0

7

I'm trying to parse my object to XML using jackson-dataformat-xml and, when i set the root namespace and parse the file, all properties of my object in the XML gives a empty namespace xmlns="". On jackson's github docs, is advise to use woodstox instead stax XML implementation to solve this but, the behavior still exists.

This is my pojo:

@JacksonXmlRootElement(namespace = "https://www.google.com.br")
public class Cliente implements Serializable {
 // Omitted
}

And my parse code:

Cliente cliente = new Cliente();
cliente.setId(new Long(1));
cliente.setNome("Tiago Cassio".toUpperCase());
cliente.setSobrenome("da Conceicao".toUpperCase());
Endereco endereco = new Endereco();
endereco.setId(new Long(1));
endereco.setLogradouro("blablabla");
endereco.setNumero("999");
endereco.setCep("99999");
endereco.setBairro("blablabla");
cliente.setEndereco(endereco);
ObjectMapper mapper = new XmlMapper();
System.out.println(mapper.writeValueAsString(cliente));

This is the XML generated:

<Cliente xmlns="https://www.google.com.br">
    <id xmlns="">1</id>
    <nome xmlns="">TIAGO CASSIO</nome>
    <sobrenome xmlns="">DA CONCEICAO</sobrenome>
    <endereco xmlns="">
        <id>1</id>
        <logradouro>blablabla</logradouro>
        <numero>999</numero>
        <cep>99999</cep>
        <bairro>blablabla</bairro>
    </endereco>
</Cliente>

Any idea where is the problem? My project is under a Spring boot version 1.3.0.M5. Thanks for all.

Pandemonium answered 13/10, 2015 at 12:54 Comment(9)
Do you have the properties of the Cliente annotated with @JacksonXmlProperty with the namespace attribute set on them? If not, can you try doing that to see if that helps?Fairlie
I've already done, still not working. The curious is when leave the object root without namespace, all properties namespaces is gone. Thanks for your help!Fluter
If your root element is in a namespace and the child elements aren't, they need to carry the namespace undeclaration to cancel the namespace declaration on the root. If everything is in no namespace, the namespace undeclaration isn't needed.Schnapps
I undestand but, i need the namespace declaration only in root element. how i can do this?Fluter
I was able to reproduce this and get around using @JacksonXmlProperty (namespace="https://www.google.com.br") on the properties. Can you check your change again?Fairlie
@TiagoCássio you do need to specify namespace for child elements too, otherwise they require namespace of "", which is why such declaration is added. @JacksonXmlProperty is one way to do that, and probably simplest. Unfortunately there is no way to specify something like "use this namespace for all properties of this class" (you could file an RFE for jackson xml module if you want, suggesting something easier)Eula
Is there any update on this question?Grizelda
@Eula Thanks, that explains it for me. I think your comment would qualify as an answer for this question.Deed
There exists already an issue for this: github.com/FasterXML/jackson-dataformat-xml/issues/18Deed

© 2022 - 2024 — McMap. All rights reserved.