1 package de.spring.webservices.rest.controller;
3 import java.util.ArrayList;
6 import java.util.concurrent.atomic.AtomicLong;
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;
23 import de.spring.webservices.domain.Car;
25 //import io.swagger.annotations.ApiOperation;
26 //import io.swagger.annotations.ApiResponse;
27 //import io.swagger.annotations.ApiResponses;
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";
35 private final AtomicLong counter = new AtomicLong();
37 // Do I want to release with Swagger dependencies?
38 // @ApiOperation(value = "getCars", nickname = "getAllCars", response = Car.class)
40 // @ApiResponse(code = 404, message ="Not found"),
41 // @ApiResponse(code = 400, message ="Invalid input")
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)));
54 // Do I want to release with Swagger dependencies?
55 // @ApiOperation(value = "getCar", nickname = "getsOneCar", response = Car.class)
57 // @ApiResponse(code = 404, message ="Not found"),
58 // @ApiResponse(code = 400, message ="Invalid input")
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) {
67 if (specialHeader != null) {
68 LOGGER.info("SPECIAL HEADER: " + specialHeader);
71 if (params.get("mirror") != null) {
72 LOGGER.info("MIRROR: " + params.get("mirror"));
75 if (params.get("window") != null) {
76 LOGGER.info("WINDOW: " + params.get("window"));
79 if (wheelParams != null) {
80 for(String wheel : wheelParams) {
86 Thread.sleep(10000); //1000 milliseconds is one second.
87 } catch(InterruptedException ex) {
88 Thread.currentThread().interrupt();
92 return new Car(counter.incrementAndGet(), String.format(TEMPLATE, id));
95 // Do I want to release with Swagger dependencies?
96 // @ApiOperation(value = "postCat", nickname = "createsNewCar", response = Car.class)
98 // @ApiResponse(code = 404, message ="Not found"),
99 // @ApiResponse(code = 400, message ="Invalid input")
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);
108 return new ResponseEntity<>(new Car(count, String.format(TEMPLATE, count)), headers, HttpStatus.CREATED);