Restlet 2.0 边学边写(七)Restlet返回xml和json数据格式

 上一次实践实现了html form来访问Restlet的PUT和DELETE方法,但返回数据都是string,仅能作为演示使用。本次实践将使各方法返回xml和json格式的数据,方便页面、程序的读取和展示。

1.xml库
首先是基础的xml数据格式。Restlet的扩展包org.restlet.ext.xml.jar提供各种xml相关类库。包中的抽象类XmlRepresentation作为父类提供接口和方法,但不能创建该父类的实例,而是需要使用SaxRepresentation和DomRepresentation类的实例来执行操作。

将Restlet安装目录\Edition Java EE\2.0.10\lib下的org.restlet.ext.xml.jar包加入Build Path。

2.Resource
修改com.sunny.restlet.order.CustomersResource类,代码如下:

Java代码 复制代码 收藏代码
  1. @Override
  2. protected Representation get() throws ResourceException {
  3. // TODO Auto-generated method stub
  4. Map customers = orderDao.getAllCustomers();
  5. DomRepresentation representation;
  6. try {
  7. representation = new DomRepresentation();
  8. Document dom = representation.getDocument();
  9. Element all = dom.createElement("customers");
  10. dom.appendChild(all);
  11. for (Object object : customers.entrySet()) {
  12. Entry<String, Customer> entry = (Entry) object;
  13. String id = entry.getKey();
  14. Customer customer = entry.getValue();
  15. Element root = dom.createElement("customer");
  16. root.setAttribute("id", id);
  17. all.appendChild(root);
  18. Element namElement = dom.createElement("name");
  19. namElement.appendChild(dom.createTextNode(customer.getName()));
  20. root.appendChild(namElement);
  21. Element addressElement = dom.createElement("address");
  22. addressElement.appendChild(dom.createTextNode(customer
  23. .getAddress()));
  24. root.appendChild(addressElement);
  25. }
  26. dom.normalizeDocument();
  27. return representation;
  28. } catch (IOException e) {
  29. // TODO Auto-generated catch block
  30. e.printStackTrace();
  31. }
  32. return null;
  33. }
  34.  
  35. @Override
  36. protected Representation post(Representation entity)
  37. throws ResourceException {
  38. // TODO Auto-generated method stub
  39. Form form = new Form(entity);
  40. String name = form.getFirstValue("name");
  41. String address = form.getFirstValue("address");
  42. Customer customer = new Customer(name, address);
  43. String id = orderDao.addCustomer(customer);
  44. customer = orderDao.getCustomerById(id);
  45. if (customer == null) {
  46. return null;
  47. }
  48. DomRepresentation representation;
  49. try {
  50. representation = new DomRepresentation();
  51. Document dom = representation.getDocument();
  52. Element root = dom.createElement("customer");
  53. root.setAttribute("id", id);
  54. dom.appendChild(root);
  55. Element namElement = dom.createElement("name");
  56. root.appendChild(namElement);
  57. namElement.appendChild(dom.createTextNode(customer.getName()));
  58. Element addressElement = dom.createElement("address");
  59. root.appendChild(addressElement);
  60. addressElement.appendChild(dom
  61. .createTextNode(customer.getAddress()));
  62. dom.normalizeDocument();
  63. return representation;
  64. } catch (IOException e) {
  65. // TODO Auto-generated catch block
  66. e.printStackTrace();
  67. }
  68. return null;
  69. }  

你可能感兴趣的:(xml,学)