COMP 655: Distributed/Operating Systems - Summer 2011
2024-04-27 17:34:23 UTC
Home
Lectures
Assignments
Books, web, tools
 
Turnitin.com
Guidelines
Writing help
Plagiarism
 
DiNo
Glassfish
RESTful client
Menu service
JAX-RS
JAXB
EJB
Java
 
Bulletin board
Contact
 Lab: RESTful web service and JAXB

Part 1 - RESTful web service

Read Chapter 3 in RESTful Java with JAX-RS and recreate the example.

Tips:

1. You need to include a web.xml file as follows:

 
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>order</display-name>
  <servlet>
    <servlet-name>Jersey REST Service</servlet-name>
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
    <init-param>
         <param-name>javax.ws.rs.Application</param-name>
         <param-value>
            com.restfully.shop.services.ShoppingApplication
         </param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>Jersey REST Service</servlet-name>
    <url-pattern>/*</url-pattern>
  </servlet-mapping>
</web-app>

2. If you experience "Invalid Media Type" error, try to replace

@Consues("application/xml")
with
@Consues("text/xml")

3. To POST a new customer, a valid xml message must be included in the request body.

 
<customer id="100">
	<first-name>John</first-name>
	<last-name>Smith</last-name>
	<street>Grant</street>
	<city>Columbus</city>
	<state>OH</state>
	<zip>43215</zip>
	<country>US</country>
</customer>

4. If a "Bad Request" error occurs, it may be due to that the "Element" type are correctly processed. You can fix it by dealing directly with the "Node" type. The revised readCustomer() method is as follows:

protected Customer readCustomer(InputStream is) {
	      try {
	         DocumentBuilder builder =
	            DocumentBuilderFactory.newInstance().newDocumentBuilder();
	         Document doc = builder.parse(is);
	         Element root = doc.getDocumentElement();
	                  
	         
	         Customer cust = new Customer();

	         if (root.getAttribute("id") != null
	                && !root.getAttribute("id").trim().equals("")) {
	            cust.setId(Integer.valueOf(root.getAttribute("id")));
	         }
	         NodeList nodes = root.getChildNodes();
	         
	         
	         for (int i = 0; i < nodes.getLength(); i++) {
	        	 Node node = nodes.item(i);
	        	 
	        	 if (node.getNodeName().equals("first-name")){
	        		 Node n=node.getFirstChild();
	        		 cust.setFirstName(n.getNodeValue());
	        	 }
	        	 else if (node.getNodeName().equals("last-name")){
	        		 Node n=node.getFirstChild();
	        		 cust.setLastName(n.getNodeValue());
	        	 }
	        	 else if (node.getNodeName().equals("street")){
	        		 Node n=node.getFirstChild();
	        		 cust.setStreet(n.getNodeValue());
	        	 }
	        	 else if (node.getNodeName().equals("city")){
	        		 Node n=node.getFirstChild();
	        		 cust.setCity(n.getNodeValue());
	        	 }
	        	 else if (node.getNodeName().equals("state")){
	        		 Node n=node.getFirstChild();
	        		 cust.setState(n.getNodeValue());
	        	 }
	        	 else if (node.getNodeName().equals("zip")){
	        		 Node n=node.getFirstChild();
	        		 cust.setZip(n.getNodeValue());
	        	 }
	        	 else if (node.getNodeName().equals("country")){
	        		 Node n=node.getFirstChild();
	        		 cust.setCountry(n.getNodeValue());
	        	 }
	         }
	         return cust;
	      }
	      catch (Exception e) {
	         throw new WebApplicationException(e,
	                       Response.Status.BAD_REQUEST);
	      }
	   }


Part 2 - XML processing with JAXB

Referrence resource

The first JAXB project comes from http://jaxb.java.net/tutorial/section_1_3-Hello-World.html, which is a part of a JAXB tutorial.

Compile the XML Schema

Save hello.xsd on your computer.

Open a commad line consol window and switch the current working directory to the directory where hello.xsd was just saved

make a new directory called hello

type the following command:

xjc -p hello -d hello hello.xsd

Three Java source files will be henerated in the "hello\generated" folder

Create a Java project

Launch your Eclipse.

File > New > Project... > Java Project

Name the project "helloxml"

I expect that you can accept default settings. We will confirm this in the lab. When asked if you want to open the Java perspective, click Yes. "Java" should appear in a highlighted box in the upper right of the Eclipse window.

Create a hello package

In the Java perspective, right-click your new project and pick New > package

Use hello as your Java package name

right-click the hello package and choose "Import..."

Select File system and navigate to the "hello\generated" folder from the previous step

Select all .java files and click Finish.

Accept defaults for the other options

GreetingListType.java, GreetingType.java, and ObjectFactory.java should appear in the package

Create a XMLtest package

Follow the same steps to create a new Java package "XMLtest"

Create a new class in this package called "hello"

 
import java.util.*;
import javax.xml.bind.*;
import hello.*;
 
public class Hello {
 
    private ObjectFactory of;
    private GreetingListType grList;
 
    public Hello(){
        of = new ObjectFactory();
        grList = of.createGreetingListType();
    }
 
    public void make( String t, String l ){
        GreetingType g = of.createGreetingType();
        g.setText( t );
        g.setLanguage( l );
        grList.getGreeting().add( g );
    }
 
    public void marshal() {
        try {
            JAXBElement<GreetingListType> gl =
                of.createGreetings( grList );
            JAXBContext jc = JAXBContext.newInstance( "hello" );
            Marshaller m = jc.createMarshaller();
            m.marshal( gl, System.out );
        } catch( JAXBException jbe ){
            // ...
        }
    }
}

Create a new class named "xmltest"

package helloxml;

import hello.*;
public class xmltest {

public static void main(String[] args) {
// TODO Auto-generated method stub
hello h = new hello();
h.make( "Bonjour, madame", "fr" );
h.make( "Hey, you", "en" );
h.marshal();

}

}

Run your program

Right click your project and pick Run As Java Application

You should see the following output in the consol window:

 
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Greetings>
  <Greeting language="fr">
    <Text>Bonjour, madame</Text>
  </Greeting>
  <Greeting language="en">
    <Text>Hey, you</Text>
  </Greeting>
</Greetings>


Part 3 - work with grades.xsd

You should now be comofrtable to create a new project to work with grades.xsd

I've created a referrence application that works with this schema: students.jar