Spring Roo
  1. Spring Roo
  2. ROO-1800

Generate a full JSON/REST interface via an entity's controller

    Details

    • Type: Improvement Improvement
    • Status: Resolved
    • Priority: Minor Minor
    • Resolution: Complete
    • Affects Version/s: 1.1.0.RELEASE
    • Fix Version/s: 1.1.1.RELEASE
    • Component/s: WEB MVC
    • Labels:
      None
    • Environment:
      Windows 7; STS 2.5.0 RELEASE

      Description

      As I understood, the ROO JSON support generates some JSON methods in the controller via the controller's aspect file. But currently, only a subset of a full JSON/REST interface (READ, CREATE, UPDATE, DELETE) is exposed.

      I suggest to provide a full interface to be able to

      *) Read one element (via GET specifying the id of an element)
      *) Read all elements (via GET)
      *) Read a number of elements via a finder (via GET specifying a finder and arguments)
      *) Create one element (via POST specifying a single JSON element)
      *) Create a number of elements (via POST specifying an array of JSON elements)
      *) Update one element (via PUT specifying the id of an element)
      *) Update a number of elements (via PUT specifying an array of JSON elements)
      *) Delete one element (via DELETE specifying the id of an element; this method is already in the controller's aspect - no specific JSON method needed)

      via corresponding JSON endpoints.

      I implemented all those methods in the controller attached. The first couple of methods is currently generated by the aspect, but I commented the out in the aspect file to have them modified in the controller to fit into the schema proposed above.

      Please also note that the finder's JSON endpoint has to be generated every time a finder is added by the user - currently no JSON endpoint seems to be generated when finders are added.

      1. json-examples.txt
        2 kB
        Stefan Schmidt
      2. NewsItem.java
        0.7 kB
        Johannes Weidl-Rektenwald
      3. NewsItemController.java
        4 kB
        Johannes Weidl-Rektenwald

        Issue Links

          Activity

          Hide
          Stefan Schmidt added a comment -

          With commit 231ee5904ac6e540055bb60a3106be6a07a370c5 I have added support for PUT, PUT (collection), DELETE, and GET (for dynamic finders). Here is an extract from Petclinic Visit controller:

              @RequestMapping(method = RequestMethod.PUT, headers = "Accept=application/json")
              public ResponseEntity<String> VisitController.updateFromJson(@RequestBody String json) {
                  if (Visit.fromJsonToVisit(json).merge() == null) {
                      return new ResponseEntity<String>(HttpStatus.NOT_FOUND);
                  }
                  return new ResponseEntity<String>(HttpStatus.OK);
              }
          
              
              @RequestMapping(value = "/jsonArray", method = RequestMethod.PUT, headers = "Accept=application/json")
              public ResponseEntity<String> VisitController.updateFromJsonArray(@RequestBody String json) {
                  for (Visit visit: Visit.fromJsonArrayToVisits(json)) {
                      if (visit.merge() == null) {
                          return new ResponseEntity<String>(HttpStatus.NOT_FOUND);
                      }
                  }
                  return new ResponseEntity<String>(HttpStatus.OK);
              }
          
              @RequestMapping(value = "/{id}", method = RequestMethod.DELETE, headers = "Accept=application/json")
              public ResponseEntity<String> VisitController.deleteFromJson(@PathVariable("id") Long id) {
                  Visit.findVisit(id).remove();
                  return new ResponseEntity<String>(HttpStatus.OK);
              }
          
              @RequestMapping(params = "find=ByDescriptionAndVisitDate", method = RequestMethod.GET, headers = "Accept=application/json")
              public String VisitController.jsonFindVisitsByDescriptionAndVisitDate(@RequestParam("description") String description, @RequestParam("visitDate") @org.springframework.format.annotation.DateTimeFormat(style = "S-") Date visitDate, Model model) {
                  return Visit.toJsonArray(Visit.findVisitsByDescriptionAndVisitDate(description, visitDate).getResultList());
              }
          

          Note the status message (as proposed by the attached controller has been omitted due to ROO-1758. I also added a DELETE mapping for completeness.

          Show
          Stefan Schmidt added a comment - With commit 231ee5904ac6e540055bb60a3106be6a07a370c5 I have added support for PUT, PUT (collection), DELETE, and GET (for dynamic finders). Here is an extract from Petclinic Visit controller: @RequestMapping(method = RequestMethod.PUT, headers = "Accept=application/json" ) public ResponseEntity< String > VisitController.updateFromJson(@RequestBody String json) { if (Visit.fromJsonToVisit(json).merge() == null ) { return new ResponseEntity< String >(HttpStatus.NOT_FOUND); } return new ResponseEntity< String >(HttpStatus.OK); } @RequestMapping(value = "/jsonArray" , method = RequestMethod.PUT, headers = "Accept=application/json" ) public ResponseEntity< String > VisitController.updateFromJsonArray(@RequestBody String json) { for (Visit visit: Visit.fromJsonArrayToVisits(json)) { if (visit.merge() == null ) { return new ResponseEntity< String >(HttpStatus.NOT_FOUND); } } return new ResponseEntity< String >(HttpStatus.OK); } @RequestMapping(value = "/{id}" , method = RequestMethod.DELETE, headers = "Accept=application/json" ) public ResponseEntity< String > VisitController.deleteFromJson(@PathVariable( "id" ) Long id) { Visit.findVisit(id).remove(); return new ResponseEntity< String >(HttpStatus.OK); } @RequestMapping(params = "find=ByDescriptionAndVisitDate" , method = RequestMethod.GET, headers = "Accept=application/json" ) public String VisitController.jsonFindVisitsByDescriptionAndVisitDate(@RequestParam( "description" ) String description, @RequestParam( "visitDate" ) @org.springframework.format.annotation.DateTimeFormat(style = "S-" ) Date visitDate, Model model) { return Visit.toJsonArray(Visit.findVisitsByDescriptionAndVisitDate(description, visitDate).getResultList()); } Note the status message (as proposed by the attached controller has been omitted due to ROO-1758 . I also added a DELETE mapping for completeness.
          Hide
          Stefan Schmidt added a comment -

          Attaching sample curl commands to illustrate the JSON functionality.

          Show
          Stefan Schmidt added a comment - Attaching sample curl commands to illustrate the JSON functionality.

            People

            • Assignee:
              Stefan Schmidt
              Reporter:
              Johannes Weidl-Rektenwald
            • Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: