{"id":2313,"date":"2017-03-01T09:25:45","date_gmt":"2017-03-01T07:25:45","guid":{"rendered":"http:\/\/hasselba.ch\/blog\/?p=2313"},"modified":"2017-03-01T09:25:45","modified_gmt":"2017-03-01T07:25:45","slug":"domino-rest-consuming-json","status":"publish","type":"post","link":"https:\/\/hasselba.ch\/blog\/?p=2313","title":{"rendered":"Domino &#038; REST: Consuming JSON"},"content":{"rendered":"<p>Consuming JSON is as easy as pie: Just create\u00a0a new method to the <a href=\"https:\/\/github.com\/hasselbach\/domino-rest-servlet\/blob\/master\/domino-rest-servlet.plugin\/src\/ch\/hasselba\/dominorestservlet\/RestApiServlet.java\" target=\"_blank\">RestApiServlet<\/a>,\u00a0 add a @POST annotation, and declare the object you want to\u00a0the parameters of the method:<\/p>\n<pre><code>\u00a0\u00a0\u00a0 @POST\r\n\u00a0\u00a0 \u00a0@Path(\"\/helloworld\/\")\r\n\u00a0\u00a0 \u00a0@Consumes(MediaType.APPLICATION_JSON)\r\n\u00a0\u00a0 \u00a0public Response postHelloWorld(HelloWorld helloWorld) {\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0return Response.ok(helloWorld, MediaType.APPLICATION_JSON).build();\r\n\u00a0\u00a0 \u00a0}<\/code><\/pre>\n<p>In this example we are using the same URI &#8222;<em>\/helloworld\/<\/em>&#8222;; because we now are using another HTTP method, there is no conflict between the two methods.<\/p>\n<p>Have a look at the parameter of the <em>postHelloWorld<\/em> method: It is the <a href=\"https:\/\/github.com\/hasselbach\/domino-rest-servlet\/blob\/master\/domino-rest-servlet.plugin\/src\/ch\/hasselba\/dominorestservlet\/HelloWorld.java\" target=\"_blank\">HelloWorld<\/a> class which now instantiated and filled automatically from the POSTed JSON data! We can access the object in Java, and in this short example the result is directly returned back in the response.<\/p>\n<p>If we now send a JSON request with a &#8222;message&#8220; field&#8230;<\/p>\n<pre><code>curl -i -X POST -d <strong>\"{\\\"message\\\": \\\"foo\\\"}\"<\/strong> \\ \r\n\u00a0\u00a0 \u00a0-H \"Content-Type: application\/json\" \\\r\n\u00a0\u00a0 \u00a0http:\/\/your.server\/dominorestservlet\/helloworld\/<\/code><\/pre>\n<p>&#8230; the response contains the message we have posted:<\/p>\n<p><a href=\"https:\/\/hasselba.ch\/blog\/wp-content\/uploads\/2017\/02\/screenshot-jsonpost.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2314\" src=\"https:\/\/hasselba.ch\/blog\/wp-content\/uploads\/2017\/02\/screenshot-jsonpost.png\" alt=\"\" width=\"680\" height=\"202\" srcset=\"https:\/\/hasselba.ch\/blog\/wp-content\/uploads\/2017\/02\/screenshot-jsonpost.png 680w, https:\/\/hasselba.ch\/blog\/wp-content\/uploads\/2017\/02\/screenshot-jsonpost-300x89.png 300w\" sizes=\"auto, (max-width: 680px) 100vw, 680px\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Consuming JSON is as easy as pie: Just create\u00a0a new method to the RestApiServlet,\u00a0 add a @POST annotation, and declare the object you want to\u00a0the parameters of the method: \u00a0\u00a0\u00a0 @POST \u00a0\u00a0 \u00a0@Path(&#8222;\/helloworld\/&#8220;) \u00a0\u00a0 \u00a0@Consumes(MediaType.APPLICATION_JSON) \u00a0\u00a0 \u00a0public Response postHelloWorld(HelloWorld helloWorld) &hellip; <a href=\"https:\/\/hasselba.ch\/blog\/?p=2313\">Weiterlesen <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[120,114,89,116,61],"tags":[118,7,11,115,60,12],"class_list":["post-2313","post","type-post","status-publish","format-standard","hentry","category-apache-wink","category-jackson","category-java","category-jee","category-rest","tag-apache-wink","tag-domino","tag-http","tag-jackson","tag-rest","tag-web"],"_links":{"self":[{"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2313","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2313"}],"version-history":[{"count":1,"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2313\/revisions"}],"predecessor-version":[{"id":2315,"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2313\/revisions\/2315"}],"wp:attachment":[{"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2313"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2313"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2313"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}