b5ae241c7837ffe90e6d8611262e543f5c3513ec
[SpringWebServicesForFun/.git] /
1 package de.spring.webservices.rest;
2
3 import java.util.ArrayList;
4 import java.util.List;
5 import java.util.Map;
6 import java.util.concurrent.atomic.AtomicLong;
7
8 import org.slf4j.Logger;
9 import org.slf4j.LoggerFactory;
10 import org.springframework.http.HttpHeaders;
11 import org.springframework.http.HttpStatus;
12 import org.springframework.http.MediaType;
13 import org.springframework.http.ResponseEntity;
14 import org.springframework.web.bind.annotation.PathVariable;
15 import org.springframework.web.bind.annotation.RequestBody;
16 import org.springframework.web.bind.annotation.RequestHeader;
17 import org.springframework.web.bind.annotation.RequestMapping;
18 import org.springframework.web.bind.annotation.RequestMethod;
19 import org.springframework.web.bind.annotation.RequestParam;
20 import org.springframework.web.bind.annotation.ResponseStatus;
21 import org.springframework.web.bind.annotation.RestController;
22
23 import de.spring.webservices.domain.Car;
24
25 //import io.swagger.annotations.ApiOperation;
26 //import io.swagger.annotations.ApiResponse;
27 //import io.swagger.annotations.ApiResponses;
28
29 @RestController
30 @RequestMapping("/api/cars/")
31 public class CarController {
32         private static final Logger LOGGER = LoggerFactory.getLogger(CarController.class);
33     private static final String TEMPLATE = "Car: %s";
34     
35     private final AtomicLong counter = new AtomicLong();
36
37 //  Do I want to release with Swagger dependencies?
38 //    @ApiOperation(value = "getCars", nickname = "getAllCars", response = Car.class)
39 //    @ApiResponses({
40 //        @ApiResponse(code =  404, message ="Not found"),
41 //        @ApiResponse(code =  400, message ="Invalid input")
42 //    })
43     @RequestMapping(produces = { MediaType.APPLICATION_JSON_UTF8_VALUE }, method = RequestMethod.GET)
44     @ResponseStatus(HttpStatus.OK)
45     public List<Car> cars() {
46         final List<Car> cars = new ArrayList<>();
47         cars.add(new Car(counter.incrementAndGet(), String.format(TEMPLATE, 1)));
48         cars.add(new Car(counter.incrementAndGet(), String.format(TEMPLATE, 2)));
49         cars.add(new Car(counter.incrementAndGet(), String.format(TEMPLATE, 3)));
50
51         return cars;
52     }
53
54 //  Do I want to release with Swagger dependencies?
55 //  @ApiOperation(value = "getCar", nickname = "getsOneCar", response = Car.class)
56 //  @ApiResponses({
57 //      @ApiResponse(code =  404, message ="Not found"),
58 //      @ApiResponse(code =  400, message ="Invalid input")
59 //  })
60     @RequestMapping(value = "{id}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, method = RequestMethod.GET)
61     @ResponseStatus(HttpStatus.OK)
62     public Car car(@RequestHeader(value = "MY_HEADER", required = false) String specialHeader,
63                 @PathVariable("id") long id,
64                 @RequestParam Map<String, String> params,
65                 @RequestParam(value = "wheel", required = false) String[] wheelParams) {
66                 
67         if (specialHeader != null) {
68                 LOGGER.info("SPECIAL HEADER: " + specialHeader);
69         }
70          
71         if (params.get("mirror") != null) {
72                 LOGGER.info("MIRROR: " + params.get("mirror")); 
73         }
74         
75         if (params.get("window") != null) {
76                 LOGGER.info("WINDOW: " + params.get("window"));
77         }
78         
79         if (wheelParams != null) {
80                 for(String wheel : wheelParams) {
81                         LOGGER.info(wheel);
82                 }
83         }
84         
85         try {
86             Thread.sleep(10000);                 //1000 milliseconds is one second.
87         } catch(InterruptedException ex) {
88             Thread.currentThread().interrupt();
89         }
90
91
92         return new Car(counter.incrementAndGet(), String.format(TEMPLATE, id));
93     }
94     
95 //  Do I want to release with Swagger dependencies?
96 //  @ApiOperation(value = "postCat", nickname = "createsNewCar", response = Car.class)
97 //  @ApiResponses({
98 //      @ApiResponse(code =  404, message ="Not found"),
99 //      @ApiResponse(code =  400, message ="Invalid input")
100 //  })
101     @RequestMapping(consumes = MediaType.APPLICATION_JSON_UTF8_VALUE,
102                 produces = MediaType.APPLICATION_JSON_UTF8_VALUE, method = RequestMethod.POST)
103     public ResponseEntity<Car> create(@RequestBody Car car) {
104         long count = counter.incrementAndGet();
105         HttpHeaders headers = new HttpHeaders();
106         headers.add(HttpHeaders.LOCATION, "/api/cars/" + count);
107         
108         return new ResponseEntity<>(new Car(count, String.format(TEMPLATE, count)), headers, HttpStatus.CREATED);
109     }
110
111 }