Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGlyn Normington2013-01-24 11:25:31 -0500
committerGlyn Normington2013-01-24 11:25:31 -0500
commitfc1108f982fd47e9815f77fdd1686520472f251f (patch)
tree46dd5766aa70bc2237b5d2f3cc06e440ea3c0877
parent3d6dd13667ccf8db163392bc06a410fe9984ce06 (diff)
downloadorg.eclipse.virgo.samples-fc1108f982fd47e9815f77fdd1686520472f251f.tar.gz
org.eclipse.virgo.samples-fc1108f982fd47e9815f77fdd1686520472f251f.tar.xz
org.eclipse.virgo.samples-fc1108f982fd47e9815f77fdd1686520472f251f.zip
tidy up
-rw-r--r--REST/org.eclipse.virgo.samples.rest/src/main/java/org/eclipse/virgo/samples/rest/RestController.java62
1 files changed, 38 insertions, 24 deletions
diff --git a/REST/org.eclipse.virgo.samples.rest/src/main/java/org/eclipse/virgo/samples/rest/RestController.java b/REST/org.eclipse.virgo.samples.rest/src/main/java/org/eclipse/virgo/samples/rest/RestController.java
index c483506..04262e9 100644
--- a/REST/org.eclipse.virgo.samples.rest/src/main/java/org/eclipse/virgo/samples/rest/RestController.java
+++ b/REST/org.eclipse.virgo.samples.rest/src/main/java/org/eclipse/virgo/samples/rest/RestController.java
@@ -29,16 +29,34 @@ import org.springframework.web.bind.annotation.ResponseBody;
/**
* <p>
* {@link RestController} is a Spring MVC controller class which handles REST requests.
+ * <p/>
+ * GET requests specify a user id, e.g.:
+ *
+ * <pre>
+ * GET /rest/users/roy HTTP/1.1
+ * Accept: application/json
+ * </pre>
+ *
+ * whereas PUT requests specify a user id, a name, and a web site, e.g.:
+ *
+ * <pre>
+ * PUT /rest/users/glyn/Glyn%20Normington/underlap.blogspot.com HTTP/1.1
+ * </pre>
+ *
+ * Note: the web site parameter must not include "http://" since Tomcat rejects the proper encoding of this string.
* </p>
- * Drive this, for example, as follows:
+ * you can use curl to drive this program as follows:
*
* <pre>
* curl -i -H "Accept: application/json" http://localhost:8080/rest/users/roy
+ * curl -i -X PUT http://localhost:8080/rest/users/glyn/Glyn%20Normington/underlap.blogspot.com
+ * curl -i -H "Accept: application/json" http://localhost:8080/rest/users/glyn
* </pre>
*
* The implementation is deliberately primitive. Please consult the following for more information:
* <p/>
* <ul>
+ * <li><a href="http://en.wikipedia.org/wiki/Representational_state_transfer">Representational State Transfer</a> (Wikipedia article)
* <li><a href="http://www.ics.uci.edu/~fielding/pubs/dissertation/top.htm">Architectural Styles and the Design of
* Network-based Software Architectures</a> (Roy Fielding's REST dissertation)</li>
* <li><a href="http://static.springsource.org/spring/docs/3.1.0.RELEASE/reference/html/mvc.html">Spring Web MVC
@@ -55,30 +73,10 @@ import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public final class RestController {
- private class Info {
-
- private String name;
-
- private String site;
-
- Info(String name, String site) {
- this.name = name;
- this.site = site;
- }
-
- String getName() {
- return this.name;
- }
-
- String getSite() {
- return this.site;
- }
- }
-
private Map<String, Info> model = Collections.synchronizedMap(new HashMap<String, Info>());
public RestController() {
- this.model.put("roy", new Info("Roy T. Fielding", "http://roy.gbiv.com"));
+ this.model.put("roy", new Info("Roy T. Fielding", "roy.gbiv.com"));
}
@RequestMapping(value = "/users/{userId}", method = RequestMethod.GET, produces = "application/json")
@@ -88,8 +86,7 @@ public final class RestController {
headers.add("Content-Type", "application/json; charset=utf-8");
Info info = model.get(userId);
if (info != null) {
- return new ResponseEntity<String>("{ \"name\" : \"" + info.getName() + "\", \"site\" : \"" + info.getSite() + "\" }", headers,
- HttpStatus.OK);
+ return new ResponseEntity<String>(info.toJson(), headers, HttpStatus.OK);
} else {
return new ResponseEntity<String>("", headers, HttpStatus.NOT_FOUND);
}
@@ -101,4 +98,21 @@ public final class RestController {
this.model.put(userId, new Info(name, site));
httpServletResponse.setStatus(HttpServletResponse.SC_OK);
}
+
+ private class Info {
+
+ private String name;
+
+ private String site;
+
+ Info(String name, String site) {
+ this.name = name;
+ this.site = site;
+ }
+
+ String toJson() {
+ return "{ \"name\" : \"" + this.name + "\", \"site\" : \"http://" + this.site + "\" }";
+ }
+ }
}
+

Back to the top