Java Reference
In-Depth Information
@Override
public String toString() {
return products.toString();
}
}
This class represents the complete catalog of products in your store. You want to return the
whole catalog as a response to a GET request from your RESTful servlet. So you identify to
JAXB that this class is the root XML element that can be marshaled to XML from Java. The
constructor sets up a couple of Product instances. Again, in the real world, this class might
be set up rather differently, depending on your Enterprise Information Systems (EIS). You just
need something for the servlet to return.
So now we get to the interesting part of this example. The servlet will return the product
catalog as XML to the output stream provided by the HttpServletResponse object
( Example 8-3 ) .
Example8-3.SimpleRestfulServlet.java
package com.soacookbook;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
/**
* Implements a simple RESTful service.
*/
public class SimpleRestServlet extends javax.servlet.http.HttpServlet
Search WWH ::




Custom Search