@RequestMapping(method = RequestMethod.GET) public ResponseEntity<List<CarRepresentation>> allCars() { List<Car> cars = carRepository.all(); HttpHeaders responseHeaders = new HttpHeaders(); responseHeaders.set("total-count", String.valueOf(cars.size())); return new ResponseEntity<>(cars.stream().map(this::toCarRepresentation).collect(toList()), responseHeaders, HttpStatus.OK); }
@GET @Produces(MediaType.APPLICATION_JSON) public Response all(@Auth User user) { List<Car> cars = carRepository.all(); List<CarRepresentation> representations = cars.stream() .map(c -> { CarRepresentation representation = new CarRepresentation(c); representation.addLink(io.github.cdelmas.spike.common.hateoas.Link.self(uriInfo.getAbsolutePathBuilder().build(c.getId()).toString())); return representation; }).collect(toList()); return Response.ok(representations) .header("total-count", String.valueOf(cars.size())) .build(); }
public List<CarRepresentation> all(Request request, Response response) { List<Car> cars = carRepository.all(); response.header("count", String.valueOf(cars.size())); response.type("application/json"); return cars.stream().map(c -> { CarRepresentation carRepresentation = new CarRepresentation(c); carRepresentation.addLink(self(request.url() + "/" + c.getId())); return carRepresentation; } ).collect(toList()); }
public void all(RoutingContext routingContext) { List<Car> all = carRepository.all(); HttpServerResponse response = routingContext.response(); response.putHeader("content-type", "application/json") .putHeader("total-count", String.valueOf(all.size())) .end(encode(all.stream().map(car -> { CarRepresentation carRepresentation = new CarRepresentation(car); carRepresentation.addLink(self(routingContext.request().absoluteURI() + "/" + car.getId())); return carRepresentation; }).collect(toList()))); }
@Get("json") public List<CarRepresentation> all() { List<io.github.cdelmas.spike.common.domain.Car> cars = carRepository.all(); getResponse().getHeaders().add("total-count", String.valueOf(cars.size())); return cars.stream().map(c -> { CarRepresentation carRepresentation = new CarRepresentation(c); carRepresentation.addLink(Link.self(new Reference(getReference()).addSegment(String.valueOf(c.getId())).toString())); return carRepresentation; }).collect(toList()); }