Hi Miguel
Have a look please at the transform feature:
http://cxf.apache.org/docs/transformationfeature.html
You can use it to get namespaces dropped, ex, a pair such as
{mynamespace}* : *
will do it.
JSONProvider has a simpler ignoreNamespaces property but if you need to
do it for XML then TransformFeature can be more effective;
By the way, are you concerned about the processing costs on the client
side ? Just curious why you'd like to drop namespaces from XML payloads
Cheers, Sergey
On 06/12/11 18:55, Miguel Martins Feitosa Filho wrote:
Dear CXF Users,
The SEI is exposing JAX-RS (REST) and JAX-WS (SOAP) web services.
@javax.jws.WebService
@javax.ws.rs.GET
The output we create has no need of namespace information as there is
only the namespace of our application.
In our case, the use of namespaces is only necessary when the
transport requires it.
1 - Our Setup
jdk1.7.0_01
CXF-2.5.0
tomcat-7.0.22
jaxb as provided by cxf in jars
2 - Our Intent:
JAX-RS/REST (JSON and XML)
We would like to have the JAX-RS output no namespace information, no
prefix and no xmlns attributes anywhere.
This I believe is the option UNSET for
(javax.xml.bind.annotation.XmlNsForm) and also defined in standards.
Please correct my understanding.
JAX-WS/SOAP
We would like as minimal namespace information as possible in the file
but that would still be correct xml and understandable by most
standard xml parsers.
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<MyApp:getBusinessMethod
xmlns:MyApp="http://myapp.mycompany.com/">
<Application>
<id>1334455</id>
</Application>
etc
</MyApp>
</soap:Body>
</soap:Envelope>
I believe this is defined in standards as an example of UNQUALIFIED XML.
Please correct me if I use any wrong terminology.
3 - Possible CXF configurations
3.a - No namespace in any annotation or in beans.xml.
No namespace in the service endpoint interface or implementation.
No namespace in package-info.java files for packages that contain the
xml model classes, e.g., classes annotated with @XmlType.
package-info.java
@javax.xml.bind.annotation.XmlSchema( elementFormDefault =
javax.xml.bind.annotation.XmlNsForm.UNQUALIFIED,
attributeFormDefault =
javax.xml.bind.annotation.XmlNsForm.UNQUALIFIED);
In this setting CXF configures the package name as the namespace.
Small tests show that in this setting no namespace information will be
output in REST which is intended.
Unfortunately I cannot make SOAP calls on the other hand work because
the model is spread out between different packages and so my output
has now as many namespaces as my model has packages and to get the
correct SOAP request xml I will have to use all these namespaces in my
input so UNQUALIFIED SOAP is not possible.
So, in essence, if I don't set my namespace I will have as many
namespaces as packages and SOAP becomes untenable.
3.b - Setting namespace in package-info.java and SEI classes.
Trying different elementFormDefault, attrbuteFormDefault settings
UNQUALIFIED and UNSET with namespace="http://myapp.mycompany.com/".
In this setting JAXB it seems is responsible for setting<tag
xmlns=""> attributes in my REST xml output and I still get ns1
prefixes even though elementFormDefault and attributeFormDefault is
UNQUALIFIED or UNSET.
This causes pain for REST users as the xml now has tags that are
qualified<ns1:tag/> and tags that are not<tagSegundo />.
Many XML Parsers cannot handle this situation and as the xml file
contains a xmlns:ns1="http://myapp.mycompany.com/" but no
xmlns="http://myapp.mycompany.com/" setting it is invalid XML.
In addition, some users just read directly the XML until they find the
one or two tags they need bypassing the creation of any other data
structure or use of any standard parsers and if the prefix changes
over time there code stops working.
The JSON output also has ns1 prefixes simply because the REST xml has it.
4. Conclusion/Questions
It seems that as soon as I set a namespace in one of the annotations
of my project, CXF and JAXB will output that information in the REST
xml.
I must define a namespace in my project as to get SOAP (JAX-WS) to be
tenable and work as intended.
Since I must define a namespace for SOAP but don't want a namespace
for REST I cannot achieve my intent (2 above) simply through
configuration either in beans.xml or in annotations.
Is the below phrase correct:
It is NOT possible to have no namespace information in REST output and
still generate correct SOAP unless the whole project is inside the
same java package and the namespace is CXF's default namespace.
The use of UNQUALIFIED or UNSET for elementFormDefault and
attributeFormDefault in package-info.java should be sufficient for no
namespace information to be output in REST even though the annotations
have the namespace setting defined. It seems though that JAXB logic is
to simply output the annotations it sees in the model so the output
gets spurious xmlns attributes and not to check if package-info.java
defines any intended behavior.
The ns1 prefix output in REST xml is done by CXF whenever the
annotations define a namespace setting in the top level xml tag,
defined for example in @XmlType even though UNQUALIFIED and UNSET are
used in package-info.java. Is this correct/intended behavior.
Example:
package-info.java
@javax.xml.bind.annotation.XmlSchema( namespace="http://myapp.mycompany.com/"
elementFormDefault = javax.xml.bind.annotation.XmlNsForm.UNQUALIFIED,
attributeFormDefault =
javax.xml.bind.annotation.XmlNsForm.UNQUALIFIED,
);
package mycompany.the.project.package;
SEI.java
class SEI {
@GET SomeInfo getSomeInfo();
}
SomeInfo.java
@XmlType(name="SomeInformation")
class SomeInfo {
String bitOfInformation;
String anotherBitOfInformation;
}
produces output
<ns1:SomeInformation xmlns:ns1="http://myapp.mycompany.com/">
<bitOfInformation>
very important information
</bitOfInformation>
<anotherBitOfInformation>
more important information
</anotherBitOfInformation>
</ns1:SomeInformation>
Would not the correct output be:
<SomeInformation xmlns="myprojectnamespace">
<bitOfInformation>
very important information
</bitOfInformation>
<anotherBitOfInformation>
more important information
</anotherBitOfInformation>
</SomeInformation>
The second xml snippet is correct because although there are no
namespace prefixes all the xml tags including bitOfInformation are
part of the namespace http://myapp.mycompany.com/.
The first xml snippet that is currently being generated by CXF/JAXB is
not correct because bitOfInformation is not part of namespace
http://myapp.mycompany.com/ although in the java code it is per the
package-info.java setting.
The first xml snippet would be corrrect if we add
xmlns="http://myapp.mycompany.com/" to the SomeInformation xml tag in
addition to the xmlns:ns1 setting.
Currently I have added a class NamespaceResponseHandler implements
ResponseHandler to JAX-RS that parses the JSON/XML output, strips the
namespace information and generates the output again. As my output is
in general small the time penalty for doing this extra step is
neglible and our users are happy because they get the expected xml but
this seems a clumsy solution.
Thank you for any light on this subject.
Miguel
--
Sergey Beryozkin
Talend Community Coders
http://coders.talend.com/
Blog: http://sberyozkin.blogspot.com