I have an Jersey API that returns Odata standard responses and consumes the same. There are specific name spaces necessary for these responses. I have a package-info.java class:
@XmlSchema(
xmlns = {
@XmlNs(namespaceURI = "http://www.w3.org/2005/Atom", prefix = ""),
@XmlNs(namespaceURI = "http://schemas.microsoft.com/ado/2007/08/dataservices", prefix = "d"),
@XmlNs(namespaceURI = "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata", prefix = "m")
},
namespace = "http://www.w3.org/2005/Atom",
elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED,
attributeFormDefault = javax.xml.bind.annotation.XmlNsForm.UNQUALIFIED)
package my.package;
import javax.xml.bind.annotation.XmlNs;
import javax.xml.bind.annotation.XmlSchema;
I use JAXB to automatically marshal and unmarshal the incoming request body and outgoing response for me. I have beans annotated for these purposes. Here is an example of one:
@XmlRootElement(name = "entry")
public class Entry
{
private String id;
private String title;
private Date updated;
private AtomLink link;
private Content content;
public Entry()
{
}
public Entry(final Content content)
{
this.content = content;
}
public Entry(final String id, final String title, final Date updated, final AtomLink link, final Content content)
{
this.id = id;
this.title = title;
this.updated = updated;
this.link = link;
this.content = content;
}
@XmlElement(name = "title")
public String getTitle()
{
return title;
}
public void setTitle(final String title)
{
this.title = title;
}
@XmlElement(name = "link")
public AtomLink getLink()
{
return link;
}
public void setLink(final AtomLink link)
{
this.link = link;
}
@XmlElement(name = "id")
public String getId()
{
return id;
}
public void setId(final String id)
{
this.id = id;
}
@XmlElement(name = "updated")
public Date getUpdated()
{
return updated;
}
public void setUpdated(final Date updated)
{
this.updated = updated;
}
@XmlElement(name = "content")
public Content getContent()
{
return content;
}
public void setContent(final Content content)
{
this.content = content;
}
}
The response comes across like this.
<ns4:entry xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices"
xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"
xmlns:ns4="http://www.w3.org/2005/Atom">
<ns4:id>TEwxaTFL</ns4:id>
<ns4:title>my resource</ns4:title>
<ns4:link href="http://127.0.0.1:8080/API/resource(TEwxaTFL)" rel="self"/>
<ns4:content type="application/xml">
<m:properties>
<d:name>temp_170_ruleset</d:name>
<d:shared>false</d:shared>
<d:autorun>false</d:autorun>
</m:properties>
</ns4:content>
</ns4:entry>
As you can see the other namespaces come across just fine. The default name space is coming back with a ns4 prefix rather than no prefix. I need it to be like this:
<entry xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices"
xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"
xmlns="http://www.w3.org/2005/Atom">
<id>TEwxaTFL</id>
<title>my resource</title>
<link href="http://127.0.0.1:8080/API/resource(TEwxaTFL)" rel="self"/>
<content type="application/xml">
<m:properties>
<d:name>temp_170_ruleset</d:name>
<d:shared>false</d:shared>
<d:autorun>false</d:autorun>
</m:properties>
</content>
</entry>
I’ve tried altering the package-info.java class to remove the name spaces:
removing
@XmlNs(namespaceURI = "http://www.w3.org/2005/Atom", prefix = ""),
and
namespace = "http://www.w3.org/2005/Atom",
and removing one at a time. Doing these things never fix the namespace prefix but affected how posts happened – mapping was not possible.
Can anyone see what I’m missing here? I really don’t want to “manually” marshal every response. So I want to avoid a NamespacePrefixMapper solution, unless I can define that without manually marshaling the response. I’ve read where this is suppose to work.
I use Jersey 1.12, JAXB 2.2
–Outcome–
Using Moxy works. I was struggling with getting it to work because the imports used were still the JaxB that was not working for me. Syntactically using Moxy is the same so there was no overhead of updating code for us. We simply needed to add the jaxb.properties file and update our imports. The only other way we got rid of the default namespaces being like this (ns1, ns4, etc.) was to use XSL on the way out – and that sucks.
Note: I’m the EclipseLink JAXB (MOXy) lead and a member of the JAXB 2 (JSR-222) expert group.
package-info
Using the following
package-infowith MOXy as the JAXB provider I was able to produce the XML that you are looking for. The line that is commented out is necessary until we finish the fix for the following bug: http://bugs.eclipse.org/365457For More Information