implementing-map.rst 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. .. _rest_services_implementing_map:
  2. Implementing a RESTful Service with Maps
  3. ========================================
  4. The previous section showed how to implement a very simple restful service.
  5. This section will show how to make use of some existing base classes in order
  6. to save time when supporting additional formats for representing resources.
  7. The class used is ``org.geoserver.rest.MapResource``. The idea with
  8. ``MapResource`` is that a resource is backed by a data structure contained
  9. by a ``java.util.Map``. With this map, the ``MapResource`` class can
  10. automatically create representations of the resource in either XML or JSON.
  11. Prerequisites
  12. --------------
  13. This section builds off the example in the previous section
  14. :ref:`rest_services_implementing`.
  15. Create a new resource class
  16. ---------------------------
  17. #. Create a new class called ``HelloMapResource`` in the package
  18. ``org.geoserver.hellorest``, which extends from ``MapResource``:
  19. .. code-block:: java
  20. package org.geoserver.hellorest;
  21. import java.util.Map;
  22. import org.geoserver.rest.MapResource;
  23. public class HelloMapResource extends MapResource {
  24. @Override
  25. public Map getMap() throws Exception {
  26. return null;
  27. }
  28. }
  29. #. The first method to implement is ``getMap()``. The purpose of this method
  30. is to create a map based data structure which represents the resource. For
  31. now a simple map will be created with a single key "message", containing
  32. the "Hello World" string:
  33. .. code-block:: java
  34. @Override
  35. public Map getMap() throws Exception {
  36. HashMap map = new HashMap();
  37. map.put( "message", "Hello World");
  38. return map;
  39. }
  40. Update the application context
  41. ------------------------------
  42. #. The next step is to update an application context and tell GeoServer
  43. about the new resource created in the previous section. Update the
  44. ``applicationContext.xml`` file so that it looks like the following:
  45. .. code-block:: xml
  46. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
  47. <beans>
  48. <bean id="hello" class="org.geoserver.hellorest.HelloResource"/>
  49. <bean id="helloMap" class="org.geoserver.hellorest.HelloMapResource"/>
  50. <bean id="helloMapping" class="org.geoserver.rest.RESTMapping">
  51. <property name="routes">
  52. <map>
  53. <entry>
  54. <key><value>/hello.{format}</value></key>
  55. <!--value>hello</value-->
  56. <value>helloMap</value>
  57. </entry>
  58. </map>
  59. </property>
  60. </bean>
  61. </beans>
  62. There are two things to note above. The first is the addition of the
  63. ``helloMap`` bean. The second is a change to the ``helloMapping`` bean,
  64. which now maps to the ``helloMap`` bean, rather than the ``hello`` bean.
  65. Test
  66. ----
  67. #. Create a new test class called ``HelloMapResourceTest`` in the package
  68. ``org.geoserver.hellorest``, which extends from
  69. ``org.geoserver.test.GeoServerTestSupport``:
  70. .. code-block:: java
  71. package org.geoserver.hellorest;
  72. import org.geoserver.test.GeoServerTestSupport;
  73. public class HelloMapResourceTest extends GeoServerTestSupport {
  74. }
  75. #. Add a test named ``testGetAsXML()`` which makes a GET request for
  76. ``/rest/hello.xml``:
  77. .. code-block:: java
  78. ...
  79. import org.w3c.dom.Document;
  80. import org.w3c.dom.Node;
  81. ...
  82. public void testGetAsXML() throws Exception {
  83. //make the request, parsing the result as a dom
  84. Document dom = getAsDOM( "/rest/hello.xml" );
  85. //print out the result
  86. print(dom);
  87. //make assertions
  88. Node message = getFirstElementByTagName( dom, "message");
  89. assertNotNull(message);
  90. assertEquals( "Hello World", message.getFirstChild().getNodeValue() );
  91. }
  92. #. Add a second test named ``testGetAsJSON()`` which makes a GET request
  93. for ``/rest/hello.json``:
  94. .. code-block:: java
  95. ...
  96. import net.sf.json.JSON;
  97. import net.sf.json.JSONObject;
  98. ...
  99. public void testGetAsJSON() throws Exception {
  100. //make the request, parsing the result into a json object
  101. JSON json = getAsJSON( "/rest/hello.json");
  102. //print out the result
  103. print(json);
  104. //make assertions
  105. assertTrue( json instanceof JSONObject );
  106. assertEquals( "Hello World", ((JSONObject)json).get( "message" ) );
  107. }
  108. #. Build and test the ``hello_test`` module::
  109. [hello_rest]% mvn clean install -Dtest=HelloMapResourceTest