From 1db6bc6301bd836af44c2063904ea63afecd0546 Mon Sep 17 00:00:00 2001 From: Gustavo Martin Morcuende Date: Thu, 5 Nov 2015 20:42:47 +0100 Subject: [PATCH] REST: header Location after POST --- SpringJava/REST/src/main/java/rest/CarController.java | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/SpringJava/REST/src/main/java/rest/CarController.java b/SpringJava/REST/src/main/java/rest/CarController.java index 87f4d01..e8d2f17 100644 --- a/SpringJava/REST/src/main/java/rest/CarController.java +++ b/SpringJava/REST/src/main/java/rest/CarController.java @@ -5,8 +5,10 @@ import java.util.ArrayList; import java.util.List; import java.util.Map; +import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; +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.RequestHeader; @@ -68,4 +70,13 @@ public class CarController { return new Car(counter.incrementAndGet(), String.format(template, id)); } + + @RequestMapping(consumes = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.POST) + public ResponseEntity create() { + HttpHeaders headers = new HttpHeaders(); + headers.add("Location", "/api/cars/" + 1); + + return new ResponseEntity<>(new Car(counter.incrementAndGet(), String.format(template, 1)), headers, HttpStatus.CREATED); + } + } -- 2.1.4