COMP 655: Distributed/Operating Systems - Winter 2015 2025-01-31 03:26:57 UTC | |||||||||||||||||||||||
| 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
Compile the XML Schema
Create a Java project Launch your Eclipse. Create a hello package
Create a XMLtest package
Run your program
Part 3 - work with grades.xsd
|