From: Gustavo Martin Morcuende Date: Thu, 5 Nov 2015 19:28:57 +0000 (+0100) Subject: REST: example with query params and custom headers X-Git-Url: https://git.gumartinm.name/?a=commitdiff_plain;h=2de59fb266ae960f3cfdbc66b5ca7680caf77b0b;p=JavaForFun REST: example with query params and custom headers --- diff --git a/SpringJava/REST/README b/SpringJava/REST/README index cb44e7d..2bd2c33 100644 --- a/SpringJava/REST/README +++ b/SpringJava/REST/README @@ -1,2 +1,3 @@ +gradle eclipse gradle build java -jar build/libs/rest-example-0.0.1.jar diff --git a/SpringJava/REST/build.gradle b/SpringJava/REST/build.gradle index 184029f..ff56d4c 100644 --- a/SpringJava/REST/build.gradle +++ b/SpringJava/REST/build.gradle @@ -8,7 +8,7 @@ buildscript { } apply plugin: 'java' -apply plugin: 'eclipse' +apply plugin: 'eclipse-wtp' apply plugin: 'spring-boot' jar { diff --git a/SpringJava/REST/src/main/java/rest/CarController.java b/SpringJava/REST/src/main/java/rest/CarController.java index 4967da0..87f4d01 100644 --- a/SpringJava/REST/src/main/java/rest/CarController.java +++ b/SpringJava/REST/src/main/java/rest/CarController.java @@ -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 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) {