Jump to: navigation, search

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

(Example)
(Example)
Line 10: Line 10:
  
 
At this point a sample student would be represented in JSON like this:
 
At this point a sample student would be represented in JSON like this:
<p>
+
 
 
{
 
{
 
     "id": 65,
 
     "id": 65,
Line 25: Line 25:
 
     ]
 
     ]
 
}  
 
}  
</p>
+
 
 
And the same student would be represented in XML like this
 
And the same student would be represented in XML like this
 
<source lang="xml">
 
<source lang="xml">

Revision as of 16:45, 11 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 an bindings file.

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:

Add a few students using POST http://localhost:8080/student.web/persistence/v1.0/jpars_example_student/entity/Student/ (you can use the sample XML or JSON shown in Step 1 as input).

  • Step 3:

In Student.java add the javax.xml.bind.annotation.XmlAttribute annotation to the name variable

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

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 missing. 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 corresponding 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 finAll 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.