1 package de.spring.webservices.rest.controller;
5 import javax.inject.Inject;
7 import org.slf4j.Logger;
8 import org.slf4j.LoggerFactory;
9 import org.springframework.data.domain.Page;
10 import org.springframework.data.domain.PageRequest;
11 import org.springframework.http.HttpHeaders;
12 import org.springframework.http.HttpStatus;
13 import org.springframework.http.MediaType;
14 import org.springframework.http.ResponseEntity;
15 import org.springframework.web.bind.annotation.PathVariable;
16 import org.springframework.web.bind.annotation.RequestBody;
17 import org.springframework.web.bind.annotation.RequestHeader;
18 import org.springframework.web.bind.annotation.RequestMapping;
19 import org.springframework.web.bind.annotation.RequestMethod;
20 import org.springframework.web.bind.annotation.RequestParam;
21 import org.springframework.web.bind.annotation.ResponseStatus;
22 import org.springframework.web.bind.annotation.RestController;
24 import de.spring.webservices.domain.Car;
25 import de.spring.webservices.rest.business.service.AwesomeBusinessLogic;
28 @RequestMapping("/api/cars/")
29 public class CarController {
30 private static final Logger LOGGER = LoggerFactory.getLogger(DeferrableCarController.class);
31 private static final int PAGE = 2;
32 private static final int PAGE_SIZE = 10;
34 private final AwesomeBusinessLogic awesomeBusinessLogic;
37 public CarController(AwesomeBusinessLogic awesomeBusinessLogic) {
38 this.awesomeBusinessLogic = awesomeBusinessLogic;
41 @RequestMapping(produces = { MediaType.APPLICATION_JSON_UTF8_VALUE }, method = RequestMethod.GET)
42 @ResponseStatus(HttpStatus.OK)
43 public Page<Car> cars() {
44 return awesomeBusinessLogic.findAll(new PageRequest(PAGE, PAGE_SIZE));
47 @RequestMapping(value = "{id}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, method = RequestMethod.GET)
48 @ResponseStatus(HttpStatus.OK)
49 public Car car(@RequestHeader(value = "MY_HEADER", required = false) String specialHeader,
50 @PathVariable("id") long id,
51 @RequestParam Map<String, String> params,
52 @RequestParam(value = "wheel", required = false) String[] wheelParams) {
54 if (specialHeader != null) {
55 LOGGER.info("SPECIAL HEADER: " + specialHeader);
58 if (params.get("mirror") != null) {
59 LOGGER.info("MIRROR: " + params.get("mirror"));
62 if (params.get("window") != null) {
63 LOGGER.info("WINDOW: " + params.get("window"));
66 if (wheelParams != null) {
67 for(String wheel : wheelParams) {
72 return awesomeBusinessLogic.findById(id);
75 @RequestMapping(consumes = MediaType.APPLICATION_JSON_UTF8_VALUE,
76 produces = MediaType.APPLICATION_JSON_UTF8_VALUE, method = RequestMethod.POST)
77 @ResponseStatus(HttpStatus.CREATED)
78 public ResponseEntity<Car> create(@RequestBody Car car) {
79 Car createdCar = awesomeBusinessLogic.create(car);
81 HttpHeaders headers = new HttpHeaders();
82 headers.add(HttpHeaders.LOCATION, "/api/cars/" + createdCar.getId());
83 return new ResponseEntity<>(createdCar, headers, HttpStatus.CREATED);