1 package de.spring.webservices.rest.client.service.impl;
4 import java.util.Arrays;
7 import org.slf4j.Logger;
8 import org.slf4j.LoggerFactory;
9 import org.springframework.beans.factory.annotation.Autowired;
10 import org.springframework.beans.factory.annotation.Value;
11 import org.springframework.http.ResponseEntity;
12 import org.springframework.stereotype.Service;
13 import org.springframework.web.client.RestTemplate;
15 import de.spring.webservices.domain.Car;
16 import de.spring.webservices.rest.client.service.CarClientService;
18 @Service("carClientService")
19 public class CarClientServiceImpl implements CarClientService {
20 private static final Logger LOGGER = LoggerFactory.getLogger(CarClientServiceImpl.class);
22 private final String apiCarsUrl;
23 private final String apiCarUrl;
24 private final RestTemplate restTemplate;
27 public CarClientServiceImpl(@Value("${url.base}${url.cars}") String apiCarsUrl,
28 @Value("${url.base}${url.car}") String apiCarUrl, RestTemplate restTemplate) {
29 this.apiCarsUrl = apiCarsUrl;
30 this.apiCarUrl = apiCarUrl;
31 this.restTemplate = restTemplate;
36 public List<Car> doGetCars() {
37 ResponseEntity<Car[]> responseEntity = restTemplate.getForEntity(apiCarsUrl, Car[].class);
39 return Arrays.asList(responseEntity.getBody());
43 public Car doGetCar(long id) {
44 ResponseEntity<Car> responseEntity = restTemplate.getForEntity(
45 apiCarUrl.replace(":id", String.valueOf(id)), Car.class);
47 return responseEntity.getBody();
51 public Car doNewCar(Car car) {
52 ResponseEntity<Car> responseEntity = restTemplate.postForEntity(apiCarsUrl, car, Car.class);
53 URI newCarLocation = responseEntity.getHeaders().getLocation();
54 LOGGER.info("new car location: " + newCarLocation.getPath());
56 return responseEntity.getBody();