REST: example with query params and custom headers
authorGustavo Martin Morcuende <gu.martinm@gmail.com>
Thu, 5 Nov 2015 19:28:57 +0000 (20:28 +0100)
committerGustavo Martin Morcuende <gu.martinm@gmail.com>
Fri, 8 Jan 2016 21:46:16 +0000 (22:46 +0100)
SpringJava/REST/README
SpringJava/REST/build.gradle
SpringJava/REST/src/main/java/rest/CarController.java

index cb44e7d..2bd2c33 100644 (file)
@@ -1,2 +1,3 @@
+gradle eclipse
 gradle build
 java -jar build/libs/rest-example-0.0.1.jar
index 184029f..ff56d4c 100644 (file)
@@ -8,7 +8,7 @@ buildscript {
 }
 
 apply plugin: 'java'
-apply plugin: 'eclipse'
+apply plugin: 'eclipse-wtp'
 apply plugin: 'spring-boot'
 
 jar {
index 4967da0..87f4d01 100644 (file)
@@ -3,9 +3,13 @@ package rest;
 import java.util.concurrent.atomic.AtomicLong;
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Map;
 
 import org.springframework.http.HttpStatus;
+import org.springframework.http.MediaType;
 import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RequestHeader;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestMethod;
 import org.springframework.web.bind.annotation.RequestParam;
@@ -13,7 +17,7 @@ import org.springframework.web.bind.annotation.ResponseStatus;
 import org.springframework.web.bind.annotation.RestController;
 
 @RestController
-@RequestMapping("/api/cars")
+@RequestMapping("/api/cars/")
 public class CarController {
 
     private static final String template = "Car: %s";
@@ -30,9 +34,31 @@ public class CarController {
         return cars;
     }
 
-    @RequestMapping(value = "/{id}", produces = { "application/json" }, method = RequestMethod.GET)
+    @RequestMapping(value = "{id}", produces = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.GET)
     @ResponseStatus(HttpStatus.OK)
-    public Car car(@PathVariable("id") long id) {
+    public Car car(@RequestHeader(value = "MY_HEADER", required = false) String specialHeader,
+               @PathVariable("id") long id,
+               @RequestParam Map<String, String> params,
+               @RequestParam(value = "wheel", required = false) String[] wheelParams) {
+               
+       if (specialHeader != null) {
+               System.out.println("SPECIAL HEADER: " + specialHeader);
+       }
+        
+       if (params.get("mirror") != null) {
+               System.out.println("MIRROR: " + params.get("mirror"));  
+       }
+       
+       if (params.get("window") != null) {
+               System.out.println("WINDOW: " + params.get("window"));
+       }
+       
+       if (wheelParams != null) {
+               for(String wheel : wheelParams) {
+                       System.out.println(wheel);
+               }
+       }
+       
         try {
             Thread.sleep(10000);                 //1000 milliseconds is one second.
         } catch(InterruptedException ex) {