chore: add basic Siren resource
authorRonald Holshausen <rholshausen@dius.com.au>
Tue, 29 Sep 2020 03:25:22 +0000 (13:25 +1000)
committerRonald Holshausen <rholshausen@dius.com.au>
Tue, 29 Sep 2020 03:25:22 +0000 (13:25 +1000)
provider/build.gradle
provider/lombok.config [new file with mode: 0644]
provider/src/main/java/io/pactflow/example/sirenprovider/SirenProviderApplication.java
provider/src/main/java/io/pactflow/example/sirenprovider/controllers/OrderController.java [new file with mode: 0644]
provider/src/main/java/io/pactflow/example/sirenprovider/models/Order.java [new file with mode: 0644]

index 6e73ef7..d9c520f 100644 (file)
@@ -2,6 +2,7 @@ plugins {
        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'
@@ -13,7 +14,10 @@ repositories {
 }
 
 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'
        }
diff --git a/provider/lombok.config b/provider/lombok.config
new file mode 100644 (file)
index 0000000..6aa51d7
--- /dev/null
@@ -0,0 +1,2 @@
+# This file is generated by the 'io.freefair.lombok' Gradle plugin
+config.stopBubbling = true
index 8384734..9ad0e11 100644 (file)
@@ -2,8 +2,10 @@ package io.pactflow.example.sirenprovider;
 
 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) {
diff --git a/provider/src/main/java/io/pactflow/example/sirenprovider/controllers/OrderController.java b/provider/src/main/java/io/pactflow/example/sirenprovider/controllers/OrderController.java
new file mode 100644 (file)
index 0000000..87f5e67
--- /dev/null
@@ -0,0 +1,36 @@
+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;
+  }
+
+}
diff --git a/provider/src/main/java/io/pactflow/example/sirenprovider/models/Order.java b/provider/src/main/java/io/pactflow/example/sirenprovider/models/Order.java
new file mode 100644 (file)
index 0000000..e9dced8
--- /dev/null
@@ -0,0 +1,10 @@
+package io.pactflow.example.sirenprovider.models;
+
+import lombok.AllArgsConstructor;
+import lombok.Data;
+
+@Data
+@AllArgsConstructor
+public class Order {
+  private Long id;
+}