Jump to: navigation, search

Difference between revisions of "EclipseLink/Examples/JPARS/SimpleJAXB"

 
Line 92: Line 92:
 
</source>
 
</source>
  
Execute the finAll named query as in the simple example: GET&nbsp;http://localhost:8080/student.web/persistence/v1.0/jpars_example_student/query/Student.findAll to execute named query findAll query as defined in the eclipselink.example.jpars.student.model.Student entity.  Inspect the output. Now the name is represented in XML as an attribute and is called fullname.  The JSON representation shows a key named fullname as attributes and elements are treated the same in JSON.
+
Execute the findAll named query as in the simple example: GET&nbsp;http://localhost:8080/student.web/persistence/v1.0/jpars_example_student/query/Student.findAll to execute named query findAll query as defined in the eclipselink.example.jpars.student.model.Student entity.  Inspect the output. Now the name is represented in XML as an attribute and is called fullname.  The JSON representation shows a key named fullname as attributes and elements are treated the same in JSON.

Latest revision as of 13:23, 15 April 2013

Overview

By adding JAXB annotations to the model classes of your JPA-RS application you can modify the format of the XML and JSON produced and accepted. JAXB annotations can be specified directly on your model classes or they can be specified in a bindings file. This example will demonstrate adding JAXB annotations to the model classes.

This example builds upon Simple JPARS Example and assumes you have successfully deployed the student application.

Example

  • Step 1:

Step through the Simple JPARS Example

At this point a sample student would be represented in JSON like this:

{
    "id": 65,
    "name": "Jane Smith",
    "courses": [
        {
            "id":101,
            "name": "math"
        },
        {
            "id":102,
            "name": "english"
        }
    ]
}

And the same student would be represented in XML like this

<student>
   <id>85</id>
   <name>Bob Smith</name>
   <courses>
      <id>101</id>
      <name>math</name>
   </courses>
   <courses>
      <id>103</id>
      <name>science</name>
   </courses>
</student>

NOTE - to switch between JSON and XML add a Header called "accept" with value either "application/xml" or "application/json"

  • Step 2:

Modify your model classes with some JAXB annotations. In Student.java add the javax.xml.bind.annotation.XmlAttribute annotation to the name variable

   @XmlAttribute(name="fullname")
   private String name;
  • Step 3: Redeploy the application
  • Step 4:

Now if you try to add some students with the same XML or JSON from step 1 you will see that the name element is now fullname. Now the acceptable JSON format to insert a student is:

{
    "id": 65,
    "fullname":"Jane Smith",
    "courses": [
        {
            "id":101,
            "name": "math"
        },
        {
            "id":102,
            "name": "english"
        }
    ]
}

And the new XML format is:

<student fullname="Bob Smith">
   <id>85</id>
   <courses>
      <id>101</id>
      <name>math</name>
   </courses>
   <courses>
      <id>103</id>
      <name>science</name>
   </courses>
</student>3

Execute the findAll named query as in the simple example: GET http://localhost:8080/student.web/persistence/v1.0/jpars_example_student/query/Student.findAll to execute named query findAll query as defined in the eclipselink.example.jpars.student.model.Student entity. Inspect the output. Now the name is represented in XML as an attribute and is called fullname. The JSON representation shows a key named fullname as attributes and elements are treated the same in JSON.