Nick Satinover 6 anos atrás
pai
commit
427f9efd26

+ 15
- 1
src/main/java/io/zipcoder/tc_spring_poll_application/controllers/PollController.java Ver arquivo

@@ -3,9 +3,13 @@ package io.zipcoder.tc_spring_poll_application.controllers;
3 3
 import io.zipcoder.tc_spring_poll_application.domain.Poll;
4 4
 import io.zipcoder.tc_spring_poll_application.repositories.PollRepository;
5 5
 import org.springframework.beans.factory.annotation.Autowired;
6
+import org.springframework.http.HttpHeaders;
6 7
 import org.springframework.http.HttpStatus;
7 8
 import org.springframework.http.ResponseEntity;
8 9
 import org.springframework.web.bind.annotation.*;
10
+import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
11
+
12
+import java.net.URI;
9 13
 
10 14
 @RestController
11 15
 public class PollController {
@@ -25,8 +29,18 @@ public class PollController {
25 29
 
26 30
     @RequestMapping(value = "/polls", method = RequestMethod.POST)
27 31
     public ResponseEntity<?> createPoll(@RequestBody Poll poll){
32
+
33
+        URI newPollUri = ServletUriComponentsBuilder
34
+                .fromCurrentRequest()
35
+                .path("{id}")
36
+                .buildAndExpand(poll.getId())
37
+                .toUri();
38
+
39
+        HttpHeaders httpHeaders = new HttpHeaders();
40
+        httpHeaders.setLocation(newPollUri);
41
+
28 42
         poll = pollRepository.save(poll);
29
-        return new ResponseEntity<>(null, HttpStatus.CREATED);
43
+        return new ResponseEntity<>(httpHeaders, HttpStatus.CREATED);
30 44
     }
31 45
 
32 46