Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Specifying XML Tag Attributes with CXF Rest Web Services

DZone's Guide to

Specifying XML Tag Attributes with CXF Rest Web Services

· Java Zone
Free Resource

Bitbucket is for the code that takes us to Mars, decodes the human genome, or drives your next car. What will your code do? Get started with Bitbucket today, it's free.

CXF comes with a reference implementation of JSR-311which is the specification for Restful web services also known as JAX-RS.We can generate XML output using Rest web services using CXF framework. Please note that CXF is a framework which is being maintained by Apache foundation.

In order to have attributes in the various XML tags being generated by using CXF, one has to use the @XmlAttribute annotation to declare attributes for a particular xml element. Usually each unique tag in XML output corresponds to a single class in a CXF REST web service.

For using the @XmlAttribute annotation, one needs to import the class  javax.xml.bind.annotation.XmlAttribute

It is worth noting that the XmlAttribute class is available to you if you are using JDK 6. If you are using anything older  than JDK 6 then you can download the JSR-311 reference implementation jar from the following location:

http://download.java.net/maven/2/javax/ws/rs/jsr311-api/0.9/

Also the @XmlAttribute annotation can be used with a field or a javabean getter property method but not with both for the same field. What we mean here is that if you have a field named "empid" and corresponding getters and setters for empid as getEmpid and setEmpid then you should either use the attribute annotation with the string variable or the getter method but not both. If used with both, an exception about duplicate attributes will thrown.

Please not that if you make a member variable as the attribute in the XML output of CXF Restful web services then that member variable will not become a child of the tag being represented by the current class.

Here is the javadoc for @XmlAttribute annotation:http://download.oracle.com/javaee/5/api/javax/xml/bind/annotation/XmlAttribute.html

From http://extreme-java.blogspot.com/2011/05/specifying-xml-tag-attributes-with-cxf.html

Bitbucket is the Git solution for professional teams who code with a purpose, not just as a hobby. Get started today, it's free.

Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}