Nick Satinover 6 年之前
父節點
當前提交
427f9efd26
共有 1 個文件被更改,包括 15 次插入1 次删除
  1. 15
    1
      src/main/java/io/zipcoder/tc_spring_poll_application/controllers/PollController.java

+ 15
- 1
src/main/java/io/zipcoder/tc_spring_poll_application/controllers/PollController.java 查看文件

3
 import io.zipcoder.tc_spring_poll_application.domain.Poll;
3
 import io.zipcoder.tc_spring_poll_application.domain.Poll;
4
 import io.zipcoder.tc_spring_poll_application.repositories.PollRepository;
4
 import io.zipcoder.tc_spring_poll_application.repositories.PollRepository;
5
 import org.springframework.beans.factory.annotation.Autowired;
5
 import org.springframework.beans.factory.annotation.Autowired;
6
+import org.springframework.http.HttpHeaders;
6
 import org.springframework.http.HttpStatus;
7
 import org.springframework.http.HttpStatus;
7
 import org.springframework.http.ResponseEntity;
8
 import org.springframework.http.ResponseEntity;
8
 import org.springframework.web.bind.annotation.*;
9
 import org.springframework.web.bind.annotation.*;
10
+import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
11
+
12
+import java.net.URI;
9
 
13
 
10
 @RestController
14
 @RestController
11
 public class PollController {
15
 public class PollController {
25
 
29
 
26
     @RequestMapping(value = "/polls", method = RequestMethod.POST)
30
     @RequestMapping(value = "/polls", method = RequestMethod.POST)
27
     public ResponseEntity<?> createPoll(@RequestBody Poll poll){
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
         poll = pollRepository.save(poll);
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