1 package de.spring.webservices.rest.controller;
3 import java.util.ArrayList;
6 import java.util.concurrent.atomic.AtomicLong;
8 import javax.ws.rs.Path;
10 import org.slf4j.Logger;
11 import org.slf4j.LoggerFactory;
12 import org.springframework.http.HttpHeaders;
13 import org.springframework.http.HttpStatus;
14 import org.springframework.http.MediaType;
15 import org.springframework.http.ResponseEntity;
16 import org.springframework.web.bind.annotation.PathVariable;
17 import org.springframework.web.bind.annotation.RequestBody;
18 import org.springframework.web.bind.annotation.RequestHeader;
19 import org.springframework.web.bind.annotation.RequestMapping;
20 import org.springframework.web.bind.annotation.RequestMethod;
21 import org.springframework.web.bind.annotation.RequestParam;
22 import org.springframework.web.bind.annotation.ResponseStatus;
23 import org.springframework.web.bind.annotation.RestController;
25 import de.spring.webservices.domain.Car;
26 import io.swagger.annotations.ApiOperation;
27 import io.swagger.annotations.ApiParam;
28 import io.swagger.annotations.ApiResponse;
29 import io.swagger.annotations.ApiResponses;
30 import io.swagger.annotations.ResponseHeader;
33 @RequestMapping("/api/cars/")
34 public class CarController {
35 private static final Logger LOGGER = LoggerFactory.getLogger(CarController.class);
36 private static final String TEMPLATE = "Car: %s";
38 private final AtomicLong counter = new AtomicLong();
40 @ApiOperation(value = "Get all available cars", nickname = "getAllCars", responseContainer="List", response = Car.class)
42 @ApiResponse(code = 404, message ="Specific getCars not found"),
43 @ApiResponse(code = 400, message ="Specific getCars invalid input")
45 @RequestMapping(produces = { MediaType.APPLICATION_JSON_UTF8_VALUE }, method = RequestMethod.GET)
46 @ResponseStatus(HttpStatus.OK)
47 public List<Car> cars() {
48 final List<Car> cars = new ArrayList<>();
49 cars.add(new Car(counter.incrementAndGet(), String.format(TEMPLATE, 1)));
50 cars.add(new Car(counter.incrementAndGet(), String.format(TEMPLATE, 2)));
51 cars.add(new Car(counter.incrementAndGet(), String.format(TEMPLATE, 3)));
56 @ApiOperation(value = "Get one car", nickname = "getOneCar", response = Car.class)
58 @ApiResponse(code = 404, message ="Specific getCar not found"),
59 @ApiResponse(code = 400, message ="Specific getCar invalid input")
61 @RequestMapping(value = "{id}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, method = RequestMethod.GET)
62 @ResponseStatus(HttpStatus.OK)
63 public Car car(@RequestHeader(value = "MY_HEADER", required = false) String specialHeader,
64 @ApiParam(name = "id", value = "Car id", required = true) @PathVariable("id") long id,
65 @RequestParam Map<String, String> params,
66 @RequestParam(value = "wheel", required = false) String[] wheelParams) {
68 if (specialHeader != null) {
69 LOGGER.info("SPECIAL HEADER: " + specialHeader);
72 if (params.get("mirror") != null) {
73 LOGGER.info("MIRROR: " + params.get("mirror"));
76 if (params.get("window") != null) {
77 LOGGER.info("WINDOW: " + params.get("window"));
80 if (wheelParams != null) {
81 for(String wheel : wheelParams) {
87 Thread.sleep(10000); //1000 milliseconds is one second.
88 } catch(InterruptedException ex) {
89 Thread.currentThread().interrupt();
93 return new Car(counter.incrementAndGet(), String.format(TEMPLATE, id));
96 @ApiOperation(code = 201, value = "Create one new car", nickname = "createNewCar")
98 @ApiResponse(code = 201, message ="Specific createCar with header",
99 responseHeaders = { @ResponseHeader(name = HttpHeaders.LOCATION) }, response = Car.class),
100 @ApiResponse(code = 404, message ="Specific createCar not found"),
101 @ApiResponse(code = 400, message ="Specific createCar invalid input")
103 @RequestMapping(consumes = MediaType.APPLICATION_JSON_UTF8_VALUE,
104 produces = MediaType.APPLICATION_JSON_UTF8_VALUE, method = RequestMethod.POST)
105 @ResponseStatus(HttpStatus.CREATED)
106 public ResponseEntity<Car> create(@RequestBody Car car) {
107 long count = counter.incrementAndGet();
108 HttpHeaders headers = new HttpHeaders();
109 headers.add(HttpHeaders.LOCATION, "/api/cars/" + count);
111 return new ResponseEntity<>(new Car(count, String.format(TEMPLATE, count)), headers, HttpStatus.CREATED);