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;
24 import io.swagger.annotations.ApiOperation;
25 import io.swagger.annotations.ApiParam;
26 import io.swagger.annotations.ApiResponse;
27 import io.swagger.annotations.ApiResponses;
28 import io.swagger.annotations.ResponseHeader;
31 @RequestMapping("/api/cars/")
32 public class CarController {
33 private static final Logger LOGGER = LoggerFactory.getLogger(CarController.class);
34 private static final String TEMPLATE = "Car: %s";
36 private final AtomicLong counter = new AtomicLong();
38 @ApiOperation(value = "Get all available cars", nickname = "getAllCars", responseContainer="List", response = Car.class)
40 @ApiResponse(code = 404, message ="Specific getCars not found"),
41 @ApiResponse(code = 400, message ="Specific getCars 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 @ApiOperation(value = "Get one car", nickname = "getOneCar", response = Car.class)
56 @ApiResponse(code = 404, message ="Specific getCar not found"),
57 @ApiResponse(code = 400, message ="Specific getCar invalid input")
59 @RequestMapping(value = "{id}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, method = RequestMethod.GET)
60 @ResponseStatus(HttpStatus.OK)
61 public Car car(@RequestHeader(value = "MY_HEADER", required = false) String specialHeader,
62 @ApiParam(name = "id", value = "Car id", required = true) @PathVariable("id") long id,
63 @RequestParam Map<String, String> params,
64 @RequestParam(value = "wheel", required = false) String[] wheelParams) {
66 if (specialHeader != null) {
67 LOGGER.info("SPECIAL HEADER: " + specialHeader);
70 if (params.get("mirror") != null) {
71 LOGGER.info("MIRROR: " + params.get("mirror"));
74 if (params.get("window") != null) {
75 LOGGER.info("WINDOW: " + params.get("window"));
78 if (wheelParams != null) {
79 for(String wheel : wheelParams) {
85 Thread.sleep(10000); //1000 milliseconds is one second.
86 } catch(InterruptedException ex) {
87 Thread.currentThread().interrupt();
91 return new Car(counter.incrementAndGet(), String.format(TEMPLATE, id));
94 @ApiOperation(code = 201, value = "Create one new car", nickname = "createNewCar")
96 @ApiResponse(code = 201, message ="Specific createCar with header",
97 responseHeaders = { @ResponseHeader(name = HttpHeaders.LOCATION) }, response = Car.class),
98 @ApiResponse(code = 404, message ="Specific createCar not found"),
99 @ApiResponse(code = 400, message ="Specific createCar invalid input")
101 @RequestMapping(consumes = MediaType.APPLICATION_JSON_UTF8_VALUE,
102 produces = MediaType.APPLICATION_JSON_UTF8_VALUE, method = RequestMethod.POST)
103 @ResponseStatus(HttpStatus.CREATED)
104 public ResponseEntity<Car> create(@RequestBody Car car) {
105 long count = counter.incrementAndGet();
106 HttpHeaders headers = new HttpHeaders();
107 headers.add(HttpHeaders.LOCATION, "/api/cars/" + count);
109 return new ResponseEntity<>(new Car(count, String.format(TEMPLATE, count)), headers, HttpStatus.CREATED);