id 'org.springframework.boot' version '2.3.4.RELEASE'
id 'io.spring.dependency-management' version '1.0.10.RELEASE'
id 'java'
+ id "io.freefair.lombok" version "5.2.1"
}
group = 'io.pactflow.example'
}
dependencies {
- implementation 'org.springframework.boot:spring-boot-starter'
+ implementation 'org.springframework.boot:spring-boot-starter-hateoas'
+ implementation 'com.jayway.jsonpath:json-path'
+ implementation 'de.ingogriebsch.hateoas:spring-hateoas-siren:1.0.0-M2'
+
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.hateoas.config.EnableHypermediaSupport;
@SpringBootApplication
+@EnableHypermediaSupport(type = EnableHypermediaSupport.HypermediaType.HAL)
public class SirenProviderApplication {
public static void main(String[] args) {
--- /dev/null
+package io.pactflow.example.sirenprovider.controllers;
+
+import io.pactflow.example.sirenprovider.models.Order;
+import org.springframework.hateoas.CollectionModel;
+import org.springframework.hateoas.EntityModel;
+import org.springframework.hateoas.RepresentationModel;
+import org.springframework.hateoas.server.ExposesResourceFor;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.PathVariable;
+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;
+
+@RestController
+@ExposesResourceFor(Order.class)
+public class OrderController {
+
+ @GetMapping("/orders")
+ public RepresentationModel<?> orders() {
+ Order order = new Order(1234L);
+ EntityModel<Order> model = EntityModel.of(order);
+ model.add(linkTo(methodOn(OrderController.class).order(1234L)).withSelfRel());
+ RepresentationModel<?> orders = CollectionModel.of(model);
+ return orders;
+ }
+
+ @GetMapping("/orders/{id}")
+ public EntityModel<Order> order(@PathVariable(value = "id", required = true) Long id) {
+ Order order = new Order(id);
+ EntityModel<Order> model = EntityModel.of(order);
+ model.add(linkTo(methodOn(OrderController.class).order(id)).withSelfRel());
+ return model;
+ }
+
+}