2e22f83495b736a1f65161004099baf65f2b4b08
[JavaForFun] /
1 package de.spring.webservices.rest.controller;
2
3 import java.util.Map;
4
5 import javax.inject.Inject;
6
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;
23
24 import de.spring.webservices.domain.Car;
25 import de.spring.webservices.rest.business.service.AwesomeBusinessLogic;
26
27 @RestController
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;
33     
34         private final AwesomeBusinessLogic awesomeBusinessLogic;
35         
36         @Inject
37         public CarController(AwesomeBusinessLogic awesomeBusinessLogic) {
38                 this.awesomeBusinessLogic = awesomeBusinessLogic;
39         }
40
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));
45     }
46
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) {
53                 
54         if (specialHeader != null) {
55                 LOGGER.info("SPECIAL HEADER: " + specialHeader);
56         }
57          
58         if (params.get("mirror") != null) {
59                 LOGGER.info("MIRROR: " + params.get("mirror")); 
60         }
61         
62         if (params.get("window") != null) {
63                 LOGGER.info("WINDOW: " + params.get("window"));
64         }
65         
66         if (wheelParams != null) {
67                 for(String wheel : wheelParams) {
68                         LOGGER.info(wheel);
69                 }
70         }
71         
72         return awesomeBusinessLogic.findById(id);
73     }
74     
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);
80         
81         HttpHeaders headers = new HttpHeaders();
82         headers.add(HttpHeaders.LOCATION, "/api/cars/" + createdCar.getId());
83                 return new ResponseEntity<>(createdCar, headers, HttpStatus.CREATED);
84     }
85
86 }