From: Ronald Holshausen Date: Tue, 29 Sep 2020 03:25:22 +0000 (+1000) Subject: chore: add basic Siren resource X-Git-Tag: wip-umbau-array-contains~33 X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=d3a04cd969afdc761963b831f0846e81eaa5d89f;p=demos%2Fexample-siren chore: add basic Siren resource --- diff --git a/provider/build.gradle b/provider/build.gradle index 6e73ef7..d9c520f 100644 --- a/provider/build.gradle +++ b/provider/build.gradle @@ -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 index 0000000..6aa51d7 --- /dev/null +++ b/provider/lombok.config @@ -0,0 +1,2 @@ +# This file is generated by the 'io.freefair.lombok' Gradle plugin +config.stopBubbling = true diff --git a/provider/src/main/java/io/pactflow/example/sirenprovider/SirenProviderApplication.java b/provider/src/main/java/io/pactflow/example/sirenprovider/SirenProviderApplication.java index 8384734..9ad0e11 100644 --- a/provider/src/main/java/io/pactflow/example/sirenprovider/SirenProviderApplication.java +++ b/provider/src/main/java/io/pactflow/example/sirenprovider/SirenProviderApplication.java @@ -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 index 0000000..87f5e67 --- /dev/null +++ b/provider/src/main/java/io/pactflow/example/sirenprovider/controllers/OrderController.java @@ -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 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(@PathVariable(value = "id", required = true) Long id) { + Order order = new Order(id); + EntityModel 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 index 0000000..e9dced8 --- /dev/null +++ b/provider/src/main/java/io/pactflow/example/sirenprovider/models/Order.java @@ -0,0 +1,10 @@ +package io.pactflow.example.sirenprovider.models; + +import lombok.AllArgsConstructor; +import lombok.Data; + +@Data +@AllArgsConstructor +public class Order { + private Long id; +}