Fiquei muito assustado com a velocidade que é possível desenvolver um pequeno serviço RESTFul com o Spring Data Rest e tudo fica mais bonito ainda sendo o banco embarcado e o servidor também embarcado.
Primeiramente, segue o pom.xml, onde será possível ver os poucos pacotes que estou usando:
| <?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> | |
| <groupId>br.com.tassioauad</groupId> | |
| <artifactId>myrest</artifactId> | |
| <version>0.1</version> | |
| <packaging>jar</packaging> | |
| <name>myrest</name> | |
| <description>RESTFul API</description> | |
| <parent> | |
| <groupId>org.springframework.boot</groupId> | |
| <artifactId>spring-boot-starter-parent</artifactId> | |
| <version>2.0.0.RELEASE</version> | |
| <relativePath/> <!-- lookup parent from repository --> | |
| </parent> | |
| <properties> | |
| <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | |
| <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> | |
| <java.version>1.8</java.version> | |
| </properties> | |
| <dependencies> | |
| <dependency> | |
| <groupId>org.springframework.boot</groupId> | |
| <artifactId>spring-boot-starter-data-jpa</artifactId> | |
| </dependency> | |
| <dependency> | |
| <groupId>org.springframework.boot</groupId> | |
| <artifactId>spring-boot-starter-data-rest</artifactId> | |
| </dependency> | |
| <dependency> | |
| <groupId>com.h2database</groupId> | |
| <artifactId>h2</artifactId> | |
| <scope>runtime</scope> | |
| </dependency> | |
| <dependency> | |
| <groupId>org.springframework.boot</groupId> | |
| <artifactId>spring-boot-starter-test</artifactId> | |
| <scope>test</scope> | |
| </dependency> | |
| </dependencies> | |
| <build> | |
| <plugins> | |
| <plugin> | |
| <groupId>org.springframework.boot</groupId> | |
| <artifactId>spring-boot-maven-plugin</artifactId> | |
| </plugin> | |
| </plugins> | |
| </build> | |
| </project> |
Estou apenas usando Spring Data JPA, Spring Data REST e o driver para conexão com o H2. Próximo passo, o arquivo de configurações application.properties fica assim:
| server.port= 9000 | |
| #-----H2 | |
| spring.h2.datasource.url=jdbc:h2:file:~/myh2 | |
| spring.h2.datasource.username=sa | |
| spring.h2.datasource.password= | |
| spring.h2.datasource.driver-class-name=org.h2.Driver | |
| spring.jpa.show-sql=true |
Próximo ponto é criar uma entidade:
| @Entity | |
| @Table(name = "person") | |
| public class Person { | |
| @Id | |
| @Column(name = "id", nullable = false) | |
| @GeneratedValue(strategy = GenerationType.IDENTITY) | |
| private Long id; | |
| @Column(name = "name", nullable = false, length = 100) | |
| private String name; | |
| @Column(name = "email", nullable = false) | |
| private String email; | |
| @Column(name = "document_nr", nullable = false, length = 15) | |
| private String documentNr; | |
| //.... | |
| } |
Agora, basta criarmos um Repository para essa entidade e colocarmos a annotation @RepositoryRestResource(path = “person”), que informará que nosso repositório será um resource Rest e será endereçado em “person/”:
| @RepositoryRestResource(path = "person") | |
| public interface PersonRepository extends PagingAndSortingRepository<Person, Long> { | |
| } |
Pronto, você já tem seu web service RESTFul com um CRUD de POST, GET, DELETE e PUT já criados e paginados também pela herança da classe PagingAndSortingRepository. Faça um teste, realize um GET em “http//:localhost:9000/person” e você verá algo como:
| { | |
| "_embedded" : { | |
| "persons" : [ ] | |
| }, | |
| "_links" : { | |
| "self" : { | |
| "href" : "http://localhost:9000/person{?page,size,sort}", | |
| "templated" : true | |
| }, | |
| "profile" : { | |
| "href" : "http://localhost:9000/profile/person" | |
| } | |
| }, | |
| "page" : { | |
| "size" : 20, | |
| "totalElements" : 0, | |
| "totalPages" : 0, | |
| "number" : 0 | |
| } | |
| } |
Agora, experimente fazer POST, PUT e DELETE seguindo o padrão de URL REST e também mediante os HATEOAS retornados.