Update for Spring Boot 2.0
This commit is contained in:
@@ -11,7 +11,6 @@ addons:
|
||||
env:
|
||||
matrix:
|
||||
- PROFILE=non-existant
|
||||
- PROFILE=spring5
|
||||
- PROFILE=spring5-next
|
||||
|
||||
cache:
|
||||
|
||||
@@ -15,15 +15,16 @@
|
||||
*/
|
||||
package org.springframework.hateoas.examples;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Optional;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import org.springframework.hateoas.Identifiable;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
@@ -51,6 +52,10 @@ class Employee implements Identifiable<Long> {
|
||||
this.role = role;
|
||||
}
|
||||
|
||||
public Optional<Long> getId() {
|
||||
return Optional.ofNullable(this.id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Just merge {@literal firstName} and {@literal lastName} together.
|
||||
*
|
||||
|
||||
@@ -65,14 +65,20 @@ class EmployeeController {
|
||||
|
||||
Employee savedEmployee = repository.save(employee);
|
||||
|
||||
return ResponseEntity
|
||||
.created(linkTo(methodOn(EmployeeController.class).findOne(savedEmployee.getId())).toUri())
|
||||
.body(assembler.toResource(savedEmployee));
|
||||
return savedEmployee.getId()
|
||||
.map(id -> ResponseEntity
|
||||
.created(linkTo(methodOn(EmployeeController.class).findOne(id)).toUri())
|
||||
.body(assembler.toResource(savedEmployee)))
|
||||
.orElse(ResponseEntity.notFound().build());
|
||||
}
|
||||
|
||||
@GetMapping(value = "/employees/{id}", produces = MediaTypes.HAL_JSON_VALUE)
|
||||
public Resource<Employee> findOne(@PathVariable Long id) {
|
||||
return assembler.toResource(repository.findOne(id));
|
||||
public ResponseEntity<Resource<Employee>> findOne(@PathVariable Long id) {
|
||||
|
||||
return repository.findById(id)
|
||||
.map(assembler::toResource)
|
||||
.map(ResponseEntity::ok)
|
||||
.orElse(ResponseEntity.notFound().build());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -15,20 +15,23 @@
|
||||
*/
|
||||
package org.springframework.hateoas.examples;
|
||||
|
||||
import lombok.AccessLevel;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import org.springframework.hateoas.Identifiable;
|
||||
|
||||
/**
|
||||
* @author Greg Turnquist
|
||||
*/
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@NoArgsConstructor(access = AccessLevel.PRIVATE)
|
||||
@Entity
|
||||
class Employee implements Identifiable<Long> {
|
||||
|
||||
@@ -42,4 +45,8 @@ class Employee implements Identifiable<Long> {
|
||||
this.name = name;
|
||||
this.role = role;
|
||||
}
|
||||
|
||||
public Optional<Long> getId() {
|
||||
return Optional.ofNullable(this.id);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -66,13 +66,17 @@ class EmployeeController {
|
||||
Employee savedEmployee = repository.save(employee);
|
||||
|
||||
return ResponseEntity
|
||||
.created(linkTo(methodOn(EmployeeController.class).findOne(savedEmployee.getId())).toUri())
|
||||
.created(savedEmployee.getId()
|
||||
.map(id -> linkTo(methodOn(EmployeeController.class).findOne(id)).toUri())
|
||||
.orElseThrow(() -> new RuntimeException("Failed to create for some reason")))
|
||||
.body(assembler.toResource(savedEmployee));
|
||||
}
|
||||
|
||||
@GetMapping(value = "/employees/{id}", produces = MediaTypes.HAL_JSON_VALUE)
|
||||
public Resource<Employee> findOne(@PathVariable Long id) {
|
||||
return assembler.toResource(repository.findOne(id));
|
||||
return repository.findById(id)
|
||||
.map(assembler::toResource)
|
||||
.orElseThrow(() -> new RuntimeException("No employee '" + id + "' found"));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -20,6 +20,8 @@ import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
@@ -74,6 +76,10 @@ class Employee implements Identifiable<Long> {
|
||||
this.role = role;
|
||||
}
|
||||
|
||||
public Optional<Long> getId() {
|
||||
return Optional.ofNullable(this.id);
|
||||
}
|
||||
|
||||
/**
|
||||
* This method will create another piece of data in the REST resource representation. These types
|
||||
* of methods are key in supporting backward compatibility.
|
||||
|
||||
@@ -70,8 +70,11 @@ class EmployeeController {
|
||||
*/
|
||||
@GetMapping(value = "/employees/{id}", produces = MediaTypes.HAL_JSON_VALUE)
|
||||
public ResponseEntity<Resource<Employee>> findOne(@PathVariable long id) {
|
||||
return ResponseEntity.ok(
|
||||
assembler.toResource(repository.findOne(id)));
|
||||
|
||||
return repository.findById(id)
|
||||
.map(assembler::toResource)
|
||||
.map(ResponseEntity::ok)
|
||||
.orElse(ResponseEntity.notFound().build());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -15,14 +15,16 @@
|
||||
*/
|
||||
package org.springframework.hateoas.examples;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.OneToOne;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import org.springframework.hateoas.Identifiable;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
@@ -53,4 +55,8 @@ class Employee implements Identifiable<Long> {
|
||||
this.role = role;
|
||||
this.manager = manager;
|
||||
}
|
||||
|
||||
public Optional<Long> getId() {
|
||||
return Optional.ofNullable(this.id);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -71,8 +71,11 @@ class EmployeeController {
|
||||
*/
|
||||
@GetMapping(value = "/employees/{id}", produces = MediaTypes.HAL_JSON_VALUE)
|
||||
public ResponseEntity<Resource<Employee>> findOne(@PathVariable long id) {
|
||||
return ResponseEntity.ok(
|
||||
assembler.toResource(repository.findOne(id)));
|
||||
|
||||
return repository.findById(id)
|
||||
.map(assembler::toResource)
|
||||
.map(ResponseEntity::ok)
|
||||
.orElse(ResponseEntity.notFound().build());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -100,10 +103,10 @@ class EmployeeController {
|
||||
@GetMapping(value = "/employees/{id}/detailed", produces = MediaTypes.HAL_JSON_VALUE)
|
||||
public ResponseEntity<Resource<EmployeeWithManager>> findDetailedEmployee(@PathVariable Long id) {
|
||||
|
||||
Employee employee = repository.findOne(id);
|
||||
|
||||
return ResponseEntity.ok(
|
||||
employeeWithManagerResourceAssembler.toResource(
|
||||
new EmployeeWithManager(employee)));
|
||||
return repository.findById(id)
|
||||
.map(EmployeeWithManager::new)
|
||||
.map(employeeWithManagerResourceAssembler::toResource)
|
||||
.map(ResponseEntity::ok)
|
||||
.orElse(ResponseEntity.notFound().build());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -47,12 +47,15 @@ class EmployeeResourceAssembler extends SimpleIdentifiableResourceAssembler<Empl
|
||||
*/
|
||||
super.addLinks(resource);
|
||||
|
||||
// Add additional links
|
||||
resource.add(linkTo(methodOn(ManagerController.class).findManager(resource.getContent().getId())).withRel("manager"));
|
||||
resource.add(linkTo(methodOn(EmployeeController.class).findDetailedEmployee(resource.getContent().getId())).withRel("detailed"));
|
||||
resource.getContent().getId()
|
||||
.ifPresent(id -> {
|
||||
// Add additional links
|
||||
resource.add(linkTo(methodOn(ManagerController.class).findManager(id)).withRel("manager"));
|
||||
resource.add(linkTo(methodOn(EmployeeController.class).findDetailedEmployee(id)).withRel("detailed"));
|
||||
|
||||
// Maintain a legacy link to support older clients not yet adjusted to the switch from "supervisor" to "manager".
|
||||
resource.add(linkTo(methodOn(SupervisorController.class).findOne(resource.getContent().getId())).withRel("supervisor"));
|
||||
// Maintain a legacy link to support older clients not yet adjusted to the switch from "supervisor" to "manager".
|
||||
resource.add(linkTo(methodOn(SupervisorController.class).findOne(id)).withRel("supervisor"));
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -33,7 +33,9 @@ public class EmployeeWithManager {
|
||||
private final Employee employee;
|
||||
|
||||
public Long getId() {
|
||||
return this.employee.getId();
|
||||
|
||||
return this.employee.getId()
|
||||
.orElseThrow(() -> new RuntimeException("Couldn't find anything."));
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
|
||||
@@ -15,17 +15,18 @@
|
||||
*/
|
||||
package org.springframework.hateoas.examples;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.OneToMany;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import org.springframework.hateoas.Identifiable;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
@@ -52,4 +53,8 @@ class Manager implements Identifiable<Long> {
|
||||
Manager(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Optional<Long> getId() {
|
||||
return Optional.ofNullable(this.id);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -65,8 +65,11 @@ class ManagerController {
|
||||
*/
|
||||
@GetMapping(value = "/managers/{id}", produces = MediaTypes.HAL_JSON_VALUE)
|
||||
ResponseEntity<Resource<Manager>> findOne(@PathVariable long id) {
|
||||
return ResponseEntity.ok(
|
||||
assembler.toResource(repository.findOne(id)));
|
||||
|
||||
return repository.findById(id)
|
||||
.map(assembler::toResource)
|
||||
.map(ResponseEntity::ok)
|
||||
.orElse(ResponseEntity.notFound().build());
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -44,8 +44,11 @@ class ManagerResourceAssembler extends SimpleIdentifiableResourceAssembler<Manag
|
||||
*/
|
||||
super.addLinks(resource);
|
||||
|
||||
// Add custom link to find all managed employees
|
||||
resource.add(linkTo(methodOn(EmployeeController.class).findEmployees(resource.getContent().getId())).withRel("employees"));
|
||||
resource.getContent().getId()
|
||||
.ifPresent(id -> {
|
||||
// Add custom link to find all managed employees
|
||||
resource.add(linkTo(methodOn(EmployeeController.class).findEmployees(id)).withRel("employees"));
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -15,11 +15,11 @@
|
||||
*/
|
||||
package org.springframework.hateoas.examples;
|
||||
|
||||
import lombok.Value;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import lombok.Value;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
|
||||
|
||||
@@ -37,7 +37,9 @@ class Supervisor {
|
||||
private final Manager manager;
|
||||
|
||||
public Long getId() {
|
||||
return this.manager.getId();
|
||||
|
||||
return this.manager.getId()
|
||||
.orElseThrow(() -> new RuntimeException("Couldn't find anything"));
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
|
||||
19
pom.xml
19
pom.xml
@@ -39,7 +39,7 @@
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>1.5.8.RELEASE</version>
|
||||
<version>2.0.0.M6</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
|
||||
@@ -54,24 +54,13 @@
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||
<java.version>1.8</java.version>
|
||||
|
||||
<spring-hateoas.version>1.0.0.BUILD-SNAPSHOT</spring-hateoas.version>
|
||||
<spring-plugin.version>2.0.0.BUILD-SNAPSHOT</spring-plugin.version>
|
||||
</properties>
|
||||
|
||||
<profiles>
|
||||
|
||||
<profile>
|
||||
<id>spring5</id>
|
||||
<properties>
|
||||
<spring.version>5.0.1.RELEASE</spring.version>
|
||||
<jackson.version>2.9.2</jackson.version>
|
||||
</properties>
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>spring-libs-milestone</id>
|
||||
<url>http://repo.spring.io/libs-milestone</url>
|
||||
</repository>
|
||||
</repositories>
|
||||
</profile>
|
||||
|
||||
<profile>
|
||||
<id>spring5-next</id>
|
||||
<properties>
|
||||
|
||||
34
security/pom.xml
Normal file
34
security/pom.xml
Normal file
@@ -0,0 +1,34 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>spring-hateoas-examples-security</artifactId>
|
||||
<name>Spring HATEOAS - Examples - Security</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.hateoas.examples</groupId>
|
||||
<artifactId>spring-hateoas-examples</artifactId>
|
||||
<version>1.0.0.BUILD-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.hateoas.examples</groupId>
|
||||
<artifactId>commons</artifactId>
|
||||
<version>1.0.0.BUILD-SNAPSHOT</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
Reference in New Issue
Block a user