Skip to content

Instantly share code, notes, and snippets.

View gregturn's full-sized avatar

Greg L. Turnquist gregturn

View GitHub Profile

Hacking with Spring Boot 2.3: Reactive Edition

@gregturn
gregturn / gist:9e05e3dd9d1bfcb3d0e5ad7468f7e4c0
Created January 12, 2018 18:13
Collection+JSON data, the way I first coded it
"data" : {
"full-name": "M. Smith",
"email" : "msmith@example.com"
}
@gregturn
gregturn / gist:3cffdb21c242c42f5351d791c06a3be5
Created January 12, 2018 18:11
Collection+JSON data, done correctly
"data" : [
{"name" : "full-name", "value" : "M. Smith", "prompt" : "Full Name"},
{"name" : "email", "value" : "msmith@example.org", "prompt" : "Email"}
]
@Data
@Value
@Builder(builderMethodName = "collectionJson")
public class CollectionJson<T> {
private String version;
private String href;
private List<Link> links;
@Singular private List<Item<T>> items;
CollectionJson collectionJson = collectionJson()
.version("1.0")
.href(resource.getId().expand().getHref())
.links(value)
.items(Collections.EMPTY_LIST)
.build();
public abstract class AffordanceModelFactory implements Plugin<MediaType> {
abstract public MediaType getMediaType();
abstract public List<AffordanceModel> findAffordanceModels(Affordance affordance, Object invocationValue);
@Override
public boolean supports(MediaType delimiter) {
return delimiter != null && delimiter.equals(this.getMediaType());
}
public interface Affordance {
/**
* HTTP method this affordance covers. For multiple methods, add multiple {@link Affordance}s.
*
* @return
*/
String getHttpMethod();
/**
@GetMapping("/employees/{id}")
public Resource<Employee> findOne(@PathVariable String id) {
// Start the affordance with the "self" link, i.e. this method.
Link findOneLink =
linkTo(methodOn(EmployeeController.class).findOne(id)).withSelfRel();
// Define final link as means to find entire collection.
Link employeesLink = linkTo(methodOn(EmployeeController.class).all()).withRel("employees");
@Value
@JsonPropertyOrder({"id", "name", "employees"})
class Supervisor {
@JsonIgnore
private final Manager manager;
public Long getId() {
return this.manager.getId();
}
@GetMapping(value = "/supervisors/{id}", produces = MediaTypes.HAL_JSON_VALUE)
public ResponseEntity<Resource<Supervisor>> findOne(@PathVariable Long id) {
Resource<Manager> managerResource = controller.findOne(id).getBody();
Resource<Supervisor> supervisorResource = new Resource<>(
new Supervisor(managerResource.getContent()),
managerResource.getLinks());
return ResponseEntity.ok(supervisorResource);
}