Skip to content
This repository has been archived by the owner on Nov 11, 2021. It is now read-only.

Update Spring Boot and Hateoas #13

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion projects/P02-VehiclesAPI/vehicles-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>
<version>2.2.6.RELEASE</version>
<relativePath/> <!-- lookup parent from com.udacity.pricing.repository -->
</parent>
<groupId>com.udacity</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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;
}
Expand All @@ -44,10 +37,10 @@ class CarController {
* @return list of vehicles
*/
@GetMapping
Resources<Resource<Car>> list() {
List<Resource<Car>> resources = carService.list().stream().map(assembler::toResource)
CollectionModel<EntityModel<Car>> list() {
List<EntityModel<Car>> models = carService.list().stream().map(assembler::toModel)
.collect(Collectors.toList());
return new Resources<>(resources,
return new CollectionModel<>(models,
linkTo(methodOn(CarController.class).list()).withSelfRel());
}

Expand All @@ -57,13 +50,13 @@ Resources<Resource<Car>> list() {
* @return all information for the requested vehicle
*/
@GetMapping("/{id}")
Resource<Car> get(@PathVariable Long id) {
EntityModel<Car> 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());
}

/**
Expand All @@ -73,14 +66,14 @@ Resource<Car> 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<Car> resource = assembler.toResource(new Car());
return ResponseEntity.created(new URI(resource.getId().expand().getHref())).body(resource);
EntityModel<Car> model = assembler.toModel(new Car());
return ResponseEntity.created(model.getLink("self").get().toUri()).body(model);
}

/**
Expand All @@ -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<Car> resource = assembler.toResource(new Car());
EntityModel<Car> resource = assembler.toModel(new Car());
return ResponseEntity.ok(resource);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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<Car, EntityModel<Car>> {

@Override
public EntityModel<Car> toModel(Car car) {
return new EntityModel<>(car,
linkTo(methodOn(CarController.class).get(car.getId())).withSelfRel(),
linkTo(methodOn(CarController.class).list()).withRel("cars"));

}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

Expand Down