Michel Frometa
Michel Frometa

Reputation: 3

Spring Data Rest and Spring MVC hateoas

I have an angular app, requesting data from a Spring Boot backend with Spring Data Rest. Requests to repositories generate responses with HATEOAS structure. But when I ask from Spring MVC controller, the response is indifferent structure(natural).

Is there any example where I can achieve the same structure on a particular controller request? I believe I should implement HATEOAS, but haven't seen a single example.

Upvotes: 0

Views: 253

Answers (1)

yejianfengblue
yejianfengblue

Reputation: 2429

Find official example here.

The return value of your controller method should be wrapped in EntityModel or CollectionModel, so the JSON should be in HAL format.

  @PostMapping("/orders")
  ResponseEntity<EntityModel<Order>> newOrder(@RequestBody Order order) {

    order.setStatus(Status.IN_PROGRESS);
    Order newOrder = orderRepository.save(order);

    return ResponseEntity //
        .created(linkTo(methodOn(OrderController.class).one(newOrder.getId())).toUri()) //
        .body(assembler.toModel(newOrder));
  }

Upvotes: 1

Related Questions