Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add basic JSON support for Example resource.

  • Loading branch information...
commit df90f9d038ea843e7953df4d486bfab4b8498a22 1 parent 1c6ca14
@skuenzli authored
View
37 sample-webapp/src/main/java/com/qualimente/reliability/web/ExampleController.java
@@ -1,30 +1,59 @@
package com.qualimente.reliability.web;
import com.qualimente.reliability.Example;
+import org.apache.log4j.Logger;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.roo.addon.web.mvc.controller.json.RooWebJson;
import org.springframework.roo.addon.web.mvc.controller.scaffold.RooWebScaffold;
import org.springframework.stereotype.Controller;
-import org.springframework.web.bind.annotation.RequestBody;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RequestMethod;
+import org.springframework.web.bind.annotation.*;
+
+import java.util.List;
@RequestMapping("/examples")
@Controller
@RooWebScaffold(path = "examples", formBackingObject = Example.class)
@RooWebJson(jsonObject = Example.class)
public class ExampleController {
+ private static final Logger LOGGER = Logger.getLogger(ExampleController.class);
@RequestMapping(method = RequestMethod.POST, headers = "Accept=application/json")
public ResponseEntity<String> createFromJson(@RequestBody String json) {
-
+ LOGGER.info("creating Example from: " + json);
Example example = Example.fromJsonToExample(json);
example.persist();
+ LOGGER.info("persisted Example: " + example.toJson());
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Type", "application/json");
return new ResponseEntity<String>(headers, HttpStatus.CREATED);
}
+
+ @RequestMapping(value = "/{id}", headers = "Accept=application/json")
+ @ResponseBody
+ public ResponseEntity<String> showJson(@PathVariable("id") Long id) {
+ LOGGER.info("requested Example: " + id);
+ Example example = Example.findExample(id);
+ HttpHeaders headers = new HttpHeaders();
+ headers.add("Content-Type", "application/json; charset=utf-8");
+ if (example == null) {
+ return new ResponseEntity<String>(headers, HttpStatus.NOT_FOUND);
+ } else {
+ LOGGER.info("returning Example: " + example.toJson());
+ return new ResponseEntity<String>(example.toJson(), headers, HttpStatus.OK);
+ }
+ }
+
+ @RequestMapping(headers = "Accept=application/json")
+ @ResponseBody
+ public ResponseEntity<String> listJson() {
+ HttpHeaders headers = new HttpHeaders();
+ headers.add("Content-Type", "application/json; charset=utf-8");
+ List<Example> result = Example.findAllExamples();
+ LOGGER.info("Examples: " + result);
+ return new ResponseEntity<String>(Example.toJsonArray(result), headers, HttpStatus.OK);
+ }
+
}
View
63 sample-webapp/src/main/java/com/qualimente/reliability/web/ExampleController_Roo_Controller_Json.aj
@@ -0,0 +1,63 @@
+// WARNING: DO NOT EDIT THIS FILE. THIS FILE IS MANAGED BY SPRING ROO.
+// You may push code into the target .java compilation unit if you wish to edit any member(s).
+
+package com.qualimente.reliability.web;
+
+import com.qualimente.reliability.Example;
+import com.qualimente.reliability.web.ExampleController;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.ResponseEntity;
+import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestMethod;
+
+privileged aspect ExampleController_Roo_Controller_Json {
+
+ @RequestMapping(value = "/jsonArray", method = RequestMethod.POST, headers = "Accept=application/json")
+ public ResponseEntity<String> ExampleController.createFromJsonArray(@RequestBody String json) {
+ for (Example example: Example.fromJsonArrayToExamples(json)) {
+ example.persist();
+ }
+ HttpHeaders headers = new HttpHeaders();
+ headers.add("Content-Type", "application/json");
+ return new ResponseEntity<String>(headers, HttpStatus.CREATED);
+ }
+
+ @RequestMapping(method = RequestMethod.PUT, headers = "Accept=application/json")
+ public ResponseEntity<String> ExampleController.updateFromJson(@RequestBody String json) {
+ HttpHeaders headers = new HttpHeaders();
+ headers.add("Content-Type", "application/json");
+ Example example = Example.fromJsonToExample(json);
+ if (example.merge() == null) {
+ return new ResponseEntity<String>(headers, HttpStatus.NOT_FOUND);
+ }
+ return new ResponseEntity<String>(headers, HttpStatus.OK);
+ }
+
+ @RequestMapping(value = "/jsonArray", method = RequestMethod.PUT, headers = "Accept=application/json")
+ public ResponseEntity<String> ExampleController.updateFromJsonArray(@RequestBody String json) {
+ HttpHeaders headers = new HttpHeaders();
+ headers.add("Content-Type", "application/json");
+ for (Example example: Example.fromJsonArrayToExamples(json)) {
+ if (example.merge() == null) {
+ return new ResponseEntity<String>(headers, HttpStatus.NOT_FOUND);
+ }
+ }
+ return new ResponseEntity<String>(headers, HttpStatus.OK);
+ }
+
+ @RequestMapping(value = "/{id}", method = RequestMethod.DELETE, headers = "Accept=application/json")
+ public ResponseEntity<String> ExampleController.deleteFromJson(@PathVariable("id") Long id) {
+ Example example = Example.findExample(id);
+ HttpHeaders headers = new HttpHeaders();
+ headers.add("Content-Type", "application/json");
+ if (example == null) {
+ return new ResponseEntity<String>(headers, HttpStatus.NOT_FOUND);
+ }
+ example.remove();
+ return new ResponseEntity<String>(headers, HttpStatus.OK);
+ }
+
+}
Please sign in to comment.
Something went wrong with that request. Please try again.