diff --git a/projects/P02-VehiclesAPI/vehicles-api/pom.xml b/projects/P02-VehiclesAPI/vehicles-api/pom.xml index 8c7028aa..180778e9 100755 --- a/projects/P02-VehiclesAPI/vehicles-api/pom.xml +++ b/projects/P02-VehiclesAPI/vehicles-api/pom.xml @@ -6,7 +6,7 @@ org.springframework.boot spring-boot-starter-parent - 2.1.5.RELEASE + 2.2.6.RELEASE com.udacity diff --git a/projects/P02-VehiclesAPI/vehicles-api/src/main/java/com/udacity/vehicles/api/CarController.java b/projects/P02-VehiclesAPI/vehicles-api/src/main/java/com/udacity/vehicles/api/CarController.java index e642c308..16429005 100755 --- a/projects/P02-VehiclesAPI/vehicles-api/src/main/java/com/udacity/vehicles/api/CarController.java +++ b/projects/P02-VehiclesAPI/vehicles-api/src/main/java/com/udacity/vehicles/api/CarController.java @@ -1,28 +1,21 @@ package com.udacity.vehicles.api; -import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo; -import static org.springframework.hateoas.mvc.ControllerLinkBuilder.methodOn; - import com.udacity.vehicles.domain.car.Car; import com.udacity.vehicles.service.CarService; -import java.net.URI; +import org.springframework.hateoas.CollectionModel; +import org.springframework.hateoas.EntityModel; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +import javax.validation.Valid; import java.net.URISyntaxException; import java.util.List; +import java.util.NoSuchElementException; import java.util.stream.Collectors; -import javax.validation.Valid; -import org.springframework.hateoas.Resource; -import org.springframework.hateoas.Resources; -import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.DeleteMapping; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.PutMapping; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; +import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.linkTo; +import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.methodOn; /** * Implements a REST-based controller for the Vehicles API. @@ -32,9 +25,9 @@ class CarController { private final CarService carService; - private final CarResourceAssembler assembler; + private final CarModelAssembler assembler; - CarController(CarService carService, CarResourceAssembler assembler) { + CarController(CarService carService, CarModelAssembler assembler) { this.carService = carService; this.assembler = assembler; } @@ -44,10 +37,10 @@ class CarController { * @return list of vehicles */ @GetMapping - Resources> list() { - List> resources = carService.list().stream().map(assembler::toResource) + CollectionModel> list() { + List> models = carService.list().stream().map(assembler::toModel) .collect(Collectors.toList()); - return new Resources<>(resources, + return new CollectionModel<>(models, linkTo(methodOn(CarController.class).list()).withSelfRel()); } @@ -57,13 +50,13 @@ Resources> list() { * @return all information for the requested vehicle */ @GetMapping("/{id}") - Resource get(@PathVariable Long id) { + EntityModel get(@PathVariable Long id) { /** * TODO: Use the `findById` method from the Car Service to get car information. * TODO: Use the `assembler` on that car and return the resulting output. * Update the first line as part of the above implementing. */ - return assembler.toResource(new Car()); + return assembler.toModel(new Car()); } /** @@ -73,14 +66,14 @@ Resource get(@PathVariable Long id) { * @throws URISyntaxException if the request contains invalid fields or syntax */ @PostMapping - ResponseEntity post(@Valid @RequestBody Car car) throws URISyntaxException { + ResponseEntity post(@Valid @RequestBody Car car) throws NoSuchElementException { /** * TODO: Use the `save` method from the Car Service to save the input car. * TODO: Use the `assembler` on that saved car and return as part of the response. * Update the first line as part of the above implementing. */ - Resource resource = assembler.toResource(new Car()); - return ResponseEntity.created(new URI(resource.getId().expand().getHref())).body(resource); + EntityModel model = assembler.toModel(new Car()); + return ResponseEntity.created(model.getLink("self").get().toUri()).body(model); } /** @@ -97,7 +90,7 @@ ResponseEntity put(@PathVariable Long id, @Valid @RequestBody Car car) { * TODO: Use the `assembler` on that updated car and return as part of the response. * Update the first line as part of the above implementing. */ - Resource resource = assembler.toResource(new Car()); + EntityModel resource = assembler.toModel(new Car()); return ResponseEntity.ok(resource); } diff --git a/projects/P02-VehiclesAPI/vehicles-api/src/main/java/com/udacity/vehicles/api/CarModelAssembler.java b/projects/P02-VehiclesAPI/vehicles-api/src/main/java/com/udacity/vehicles/api/CarModelAssembler.java new file mode 100755 index 00000000..c1ccd70e --- /dev/null +++ b/projects/P02-VehiclesAPI/vehicles-api/src/main/java/com/udacity/vehicles/api/CarModelAssembler.java @@ -0,0 +1,24 @@ +package com.udacity.vehicles.api; + +import com.udacity.vehicles.domain.car.Car; +import org.springframework.hateoas.EntityModel; +import org.springframework.hateoas.server.RepresentationModelAssembler; +import org.springframework.stereotype.Component; + +import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.linkTo; +import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.methodOn; + +/** + * Maps the CarController to the Car class using HATEOAS + */ +@Component +public class CarModelAssembler implements RepresentationModelAssembler> { + + @Override + public EntityModel toModel(Car car) { + return new EntityModel<>(car, + linkTo(methodOn(CarController.class).get(car.getId())).withSelfRel(), + linkTo(methodOn(CarController.class).list()).withRel("cars")); + + } +} diff --git a/projects/P02-VehiclesAPI/vehicles-api/src/main/java/com/udacity/vehicles/api/CarResourceAssembler.java b/projects/P02-VehiclesAPI/vehicles-api/src/main/java/com/udacity/vehicles/api/CarResourceAssembler.java deleted file mode 100755 index 9b70ba74..00000000 --- a/projects/P02-VehiclesAPI/vehicles-api/src/main/java/com/udacity/vehicles/api/CarResourceAssembler.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.udacity.vehicles.api; - -import com.udacity.vehicles.domain.car.Car; -import org.springframework.hateoas.Resource; -import org.springframework.hateoas.ResourceAssembler; -import org.springframework.stereotype.Component; - -import static org.springframework.hateoas.mvc.ControllerLinkBuilder.*; - -/** - * Maps the CarController to the Car class using HATEOAS - */ -@Component -public class CarResourceAssembler implements ResourceAssembler> { - - @Override - public Resource toResource(Car car) { - return new Resource<>(car, - linkTo(methodOn(CarController.class).get(car.getId())).withSelfRel(), - linkTo(methodOn(CarController.class).list()).withRel("cars")); - - } -} diff --git a/projects/P02-VehiclesAPI/vehicles-api/src/main/java/com/udacity/vehicles/api/ErrorController.java b/projects/P02-VehiclesAPI/vehicles-api/src/main/java/com/udacity/vehicles/api/ErrorController.java index 79fe6141..dcc2e6ea 100755 --- a/projects/P02-VehiclesAPI/vehicles-api/src/main/java/com/udacity/vehicles/api/ErrorController.java +++ b/projects/P02-VehiclesAPI/vehicles-api/src/main/java/com/udacity/vehicles/api/ErrorController.java @@ -2,7 +2,6 @@ import java.util.List; import java.util.stream.Collectors; -import org.springframework.hateoas.VndErrors; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; diff --git a/projects/P02-VehiclesAPI/vehicles-api/src/test/java/com/udacity/vehicles/api/CarControllerTest.java b/projects/P02-VehiclesAPI/vehicles-api/src/test/java/com/udacity/vehicles/api/CarControllerTest.java index 7f84e4be..0492226e 100755 --- a/projects/P02-VehiclesAPI/vehicles-api/src/test/java/com/udacity/vehicles/api/CarControllerTest.java +++ b/projects/P02-VehiclesAPI/vehicles-api/src/test/java/com/udacity/vehicles/api/CarControllerTest.java @@ -80,8 +80,8 @@ public void createCar() throws Exception { mvc.perform( post(new URI("/cars")) .content(json.write(car).getJson()) - .contentType(MediaType.APPLICATION_JSON_UTF8) - .accept(MediaType.APPLICATION_JSON_UTF8)) + .contentType(MediaType.APPLICATION_JSON) + .accept(MediaType.APPLICATION_JSON)) .andExpect(status().isCreated()); }