You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by "KARR, DAVID (ATTCINW)" <dk...@att.com> on 2009/09/11 17:40:24 UTC

How to make relatively simple change in XML mapping

I have a relatively simple prototype using the REST features of CXF, and
using JAXB out of the box to marshal a response to XML and JSON.  This
is working fine.

I'm now trying to experiment with simple changes to the structure of the
marshaled XML, using "@Xml..." annotations.  I assume that's what those
are for.  I tried asking about this on the Metro JAXB web forum, but I
got no response.  Is there another good forum for asking questions about
this?

Initially, my code looks like this:

@XmlRootElement(name = "Item")
public class Item {
  private String  id;
  private String  title;
  private String  description;
  private List<String>  features  = new ArrayList<String>();

  // ... getters/setters
	
  public void addFeature(String feature) { getFeatures().add(feature); }
}

Another class:

@GET
@Path("/item/{id}")
@Produces({"application/xml", "application/json"})
public Item getItem(@PathParam("id") String id) {
  Item item	= new Item();
  item.setId(id);
  item.setTitle("abc");
  item.setDescription("def");
  item.addFeature("123");
  item.addFeature("456");
  return item;
}

When I send a request to ".../item/1", I get back:

<Item>
 <description>def</description>
 <features>123</features>
 <features>456</features>
 <id>1</id>
 <title>abc</title>
</Item>

I'd really prefer to get:

<Item>
 <description>def</description>
 <features>
  <feature>123</feature>
  <feature>456</feature>
 </features>
 <id>1</id>
 <title>abc</title>
</Item>

I've played around with several of the "@Xml" annotations on the
"features" field, but they just give me exceptions.  For instance, I
just tried a simple case of adding '@XMLElement("feature")' just before
that field, and it gave me this exception:

WARNING: com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 1
counts of IllegalAnnotationExceptions
Class has two properties of the same name "features"
        this problem is related to the following location:
                at public java.util.List
com.att.ecom.catalog.Item.getFeatures()
                at com.att.ecom.catalog.Item
        this problem is related to the following location:
                at private java.util.List
com.att.ecom.catalog.Item.features
                at com.att.ecom.catalog.Item

RE: How to make relatively simple change in XML mapping

Posted by "KARR, DAVID (ATTCINW)" <dk...@att.com>.
> -----Original Message-----
> From: Daniel Kulp [mailto:dkulp@apache.org]
> 
> I think you need to add an @XmlElementWrapper annotation to the
> features
> field.   The XmlElement would name the element and the
> XmlElementWrapper would
> provide a wrapper element.

I was about to say that I'd already tried that, and it failed, but I
finally carefully read the exception, which made me realize that putting
the annotation on the field wasn't appropriate. After I put it on the
getter method, it at least got me a little closer.  I think I'll try the
next question on the jaxb list.

Thanks.

Re: How to make relatively simple change in XML mapping

Posted by Daniel Kulp <dk...@apache.org>.
I think you need to add an @XmlElementWrapper annotation to the features 
field.   The XmlElement would name the element and the XmlElementWrapper would 
provide a wrapper element.

Dan


On Fri September 11 2009 11:40:24 am KARR, DAVID (ATTCINW) wrote:
> I have a relatively simple prototype using the REST features of CXF, and
> using JAXB out of the box to marshal a response to XML and JSON.  This
> is working fine.
> 
> I'm now trying to experiment with simple changes to the structure of the
> marshaled XML, using "@Xml..." annotations.  I assume that's what those
> are for.  I tried asking about this on the Metro JAXB web forum, but I
> got no response.  Is there another good forum for asking questions about
> this?
> 
> Initially, my code looks like this:
> 
> @XmlRootElement(name = "Item")
> public class Item {
>   private String  id;
>   private String  title;
>   private String  description;
>   private List<String>  features  = new ArrayList<String>();
> 
>   // ... getters/setters
> 
>   public void addFeature(String feature) { getFeatures().add(feature); }
> }
> 
> Another class:
> 
> @GET
> @Path("/item/{id}")
> @Produces({"application/xml", "application/json"})
> public Item getItem(@PathParam("id") String id) {
>   Item item	= new Item();
>   item.setId(id);
>   item.setTitle("abc");
>   item.setDescription("def");
>   item.addFeature("123");
>   item.addFeature("456");
>   return item;
> }
> 
> When I send a request to ".../item/1", I get back:
> 
> <Item>
>  <description>def</description>
>  <features>123</features>
>  <features>456</features>
>  <id>1</id>
>  <title>abc</title>
> </Item>
> 
> I'd really prefer to get:
> 
> <Item>
>  <description>def</description>
>  <features>
>   <feature>123</feature>
>   <feature>456</feature>
>  </features>
>  <id>1</id>
>  <title>abc</title>
> </Item>
> 
> I've played around with several of the "@Xml" annotations on the
> "features" field, but they just give me exceptions.  For instance, I
> just tried a simple case of adding '@XMLElement("feature")' just before
> that field, and it gave me this exception:
> 
> WARNING: com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 1
> counts of IllegalAnnotationExceptions
> Class has two properties of the same name "features"
>         this problem is related to the following location:
>                 at public java.util.List
> com.att.ecom.catalog.Item.getFeatures()
>                 at com.att.ecom.catalog.Item
>         this problem is related to the following location:
>                 at private java.util.List
> com.att.ecom.catalog.Item.features
>                 at com.att.ecom.catalog.Item
> 

-- 
Daniel Kulp
dkulp@apache.org
http://www.dankulp.com/blog

Re: How to make relatively simple change in XML mapping

Posted by Sergey Beryozkin <se...@iona.com>.
You may want to post questions to

http://www.nabble.com/java.net---jaxb-users-f13500.html

You might also want to consider registering a custom XMLStreamWriter which
will be given a chance to changed the way XML is produced
cheers, Sergey


KARR, DAVID (ATTCINW) wrote:
> 
> I have a relatively simple prototype using the REST features of CXF, and
> using JAXB out of the box to marshal a response to XML and JSON.  This
> is working fine.
> 
> I'm now trying to experiment with simple changes to the structure of the
> marshaled XML, using "@Xml..." annotations.  I assume that's what those
> are for.  I tried asking about this on the Metro JAXB web forum, but I
> got no response.  Is there another good forum for asking questions about
> this?
> 
> Initially, my code looks like this:
> 
> @XmlRootElement(name = "Item")
> public class Item {
>   private String  id;
>   private String  title;
>   private String  description;
>   private List<String>  features  = new ArrayList<String>();
> 
>   // ... getters/setters
> 	
>   public void addFeature(String feature) { getFeatures().add(feature); }
> }
> 
> Another class:
> 
> @GET
> @Path("/item/{id}")
> @Produces({"application/xml", "application/json"})
> public Item getItem(@PathParam("id") String id) {
>   Item item	= new Item();
>   item.setId(id);
>   item.setTitle("abc");
>   item.setDescription("def");
>   item.addFeature("123");
>   item.addFeature("456");
>   return item;
> }
> 
> When I send a request to ".../item/1", I get back:
> 
> <Item>
>  <description>def</description>
>  <features>123</features>
>  <features>456</features>
>  <id>1</id>
>  <title>abc</title>
> </Item>
> 
> I'd really prefer to get:
> 
> <Item>
>  <description>def</description>
>  <features>
>   <feature>123</feature>
>   <feature>456</feature>
>  </features>
>  <id>1</id>
>  <title>abc</title>
> </Item>
> 
> I've played around with several of the "@Xml" annotations on the
> "features" field, but they just give me exceptions.  For instance, I
> just tried a simple case of adding '@XMLElement("feature")' just before
> that field, and it gave me this exception:
> 
> WARNING: com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 1
> counts of IllegalAnnotationExceptions
> Class has two properties of the same name "features"
>         this problem is related to the following location:
>                 at public java.util.List
> com.att.ecom.catalog.Item.getFeatures()
>                 at com.att.ecom.catalog.Item
>         this problem is related to the following location:
>                 at private java.util.List
> com.att.ecom.catalog.Item.features
>                 at com.att.ecom.catalog.Item
> 
> 

-- 
View this message in context: http://www.nabble.com/How-to-make-relatively-simple-change-in-XML-mapping-tp25403400p25404301.html
Sent from the cxf-user mailing list archive at Nabble.com.