Mitja Rogl
Mitja Rogl

Reputation: 884

Rest - order GET response xml

I' m getting this simple xml from rest GET method. Restful service is written in java.

   <gear>
      <price>355.95929799818884</price>
      <idGear>1</idGear>
      <year>1985</year>
      <name>GPS</name>
  </gear>

My question is how to change the order of xml? I want to have idGear as a first node. Is this basic or more complex than I think? Thanks for help?

UPDATE:

@XmlRootElement
@Entity(name="gear")
public class Gear{

    @Id
    @GeneratedValue
    int idGear;
    private String name;
    private int year;
    private double price;
      //getters, setters

Upvotes: 1

Views: 1364

Answers (3)

NamingException
NamingException

Reputation: 2404

Its no where related to Rest. It can be achieved at JAXB conversion only by using proporder in @XmlType annotation like.

@XmlType(propOrder={"idGear", "name", "year", "price"})

Upvotes: 2

Nazar Annagurban
Nazar Annagurban

Reputation: 336

@XmlType annotation has an attribute propOrder that can be used. Here is from the documentation:

The annotation element propOrder() can be used to customize the content model to be xs:all or xs:sequence. It is used for specifying the order of XML elements in xs:sequence.

Javadoc for XmlType

Upvotes: 2

bdoughan
bdoughan

Reputation: 149007

You can use the @XmlType annotation and set a propOrder to control the order that elements are marshalled to XML.

@XmlRootElement
@XmlType(propOrder={"idGear", "name", "year", "price"})
@Entity(name="gear")
public class Gear{

    @Id
    @GeneratedValue
    int idGear;
    private String name;
    private int year;
    private double price;

}

For More Information

Upvotes: 5

Related Questions