Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Generating XML In Jax-rs  RSS feed

 
Prabhash Mishra
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello I want to generate following XML using Jax-rs:

My pojo class is:

But with this class <attribute name=""></attribute> is not coming.Client want exact format.I am new to JAX-RS can anybody help me out.
 
Stephan van Hulst
Saloon Keeper
Posts: 7706
141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Prabhash, welcome to CodeRanch!

When you post code, please use the [code] tags to make it more easily readable. I will add them for you this time.

Your intended XML does not mach the annotations you're using. You want elements that are named "attribute", but you're using @XmlAttribute, which adds attributes to an element. If you want to add a whole bunch of elements named "attribute", you should try this:

Note that you don't need @XmlRootElement on either Order or Attribute, because they're not root elements. They are part of your order history class, which should have an @XmlRootElement annotation.
 
Prabhash Mishra
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Sir,
             I trying to set the value of attribute if element but setter og Attribute class is not coming on my Order class.i was able to create the object of Attribute class but its setter and getter methods are not accessible from Order Class.




 
Prabhash Mishra
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for typing error.Problem with my keyboard.I was setting value of attribute like name="orderId" or name="status" and adding it to List<Attribute> attributes and finally returning it on Order class but i was able to create object of Attribute class but SetValue or any getter setter method of Attribut class was not coming on Order class.
 
Prabhash Mishra
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One thing i forgot i omitted final modifire to both Order and Attribute class
 
Stephan van Hulst
Saloon Keeper
Posts: 7706
141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You shouldn't use backing fields for all your properties. Instead, the setters should add an attribute to your attributes collection, and your getters should search for the appropriate attribute in the collection.

To let the marshaller access getters rather than fields, use PROPERTY instead of FIELD for your access type. You should also use annotations to ignore all of the getters other than the attributes collection, otherwise they will end up in your XML twice.
 
Prabhash Mishra
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
   Thanks for your support i achieve the desired format by writing Adapter class.Thanks 
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!