问题
这里使用Spring Data Neo4j来实现REST服务,不过这个是使用PagingAndSortingRepository来实现的。
Neo4j服务器
按照之前的neo4j的Hello World文章,准备一个neo4j服务器即可。
Spring Initializr
访问https://start.spring.io/网页,完成工程的初始化:
Neo4j用户
src/main/resources/application.properties
:
spring.neo4j.uri=bolt://localhost:7687
spring.neo4j.authentication.username=neo4j
spring.neo4j.authentication.password=neo4j
Entity
Person.java
package com.example.neo4j2.domain;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.neo4j.core.schema.GeneratedValue;
import org.springframework.data.neo4j.core.schema.Id;
import org.springframework.data.neo4j.core.schema.Node;
@Node
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Data
public class Person {
@Id
@GeneratedValue
private Long id;
private String firstName;
private String lastName;
}
注意这里需要使用lombok库了。需要添加如下依赖:
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
具体使用可以参考《Gradle中lombok使用》。
Repository
添加pom.xml依赖库:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
创建repository:
package com.example.neo4j2.repository;
import com.example.neo4j2.domain.Person;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
import java.util.List;
@RepositoryRestResource(collectionResourceRel = "people", path = "people")
public interface PersonRepository extends PagingAndSortingRepository<Person, Long> {
List<Person> findByLastName(@Param("name") String name);
}
Application
package com.example.neo4j2;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.neo4j.repository.config.EnableNeo4jRepositories;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@EnableTransactionManagement
@EnableNeo4jRepositories
@SpringBootApplication
public class Neo4j2Application {
public static void main(String[] args) {
SpringApplication.run(Neo4j2Application.class, args);
}
}
到这里,我们就构建好一个按照HAL方式的REST API了。
CRUD
查看整体:
curl http://localhost:8080
{
"_links" : {
"people" : {
"href" : "http://localhost:8080/people{?page,size,sort}",
"templated" : true
},
"profile" : {
"href" : "http://localhost:8080/profile"
}
}
}%
查看所有people:
curl http://localhost:8080/people
{
"_embedded" : {
"people" : [ ]
},
"_links" : {
"self" : {
"href" : "http://localhost:8080/people"
},
"profile" : {
"href" : "http://localhost:8080/profile/people"
},
"search" : {
"href" : "http://localhost:8080/people/search"
}
},
"page" : {
"size" : 20,
"totalElements" : 0,
"totalPages" : 0,
"number" : 0
}
}%
添加:
curl -i -X POST -H "Content-Type:application/json" -d '{ "firstName" : "Frodo", "lastName" : "Baggins" }' http://localhost:8080/people
HTTP/1.1 201
Vary: Origin
Vary: Access-Control-Request-Method
Vary: Access-Control-Request-Headers
Location: http://localhost:8080/people/2
Content-Type: application/hal+json
Transfer-Encoding: chunked
Date: Thu, 24 Mar 2022 06:37:06 GMT
{
"firstName" : "Frodo",
"lastName" : "Baggins",
"_links" : {
"self" : {
"href" : "http://localhost:8080/people/2"
},
"person" : {
"href" : "http://localhost:8080/people/2"
}
}
}%
查看:
curl http://localhost:8080/people
{
"_embedded" : {
"people" : [ {
"firstName" : "Frodo",
"lastName" : "Baggins",
"_links" : {
"self" : {
"href" : "http://localhost:8080/people/2"
},
"person" : {
"href" : "http://localhost:8080/people/2"
}
}
} ]
},
"_links" : {
"self" : {
"href" : "http://localhost:8080/people"
},
"profile" : {
"href" : "http://localhost:8080/profile/people"
},
"search" : {
"href" : "http://localhost:8080/people/search"
}
},
"page" : {
"size" : 20,
"totalElements" : 1,
"totalPages" : 1,
"number" : 0
}
}%
查询刚刚添加的一个:
curl http://localhost:8080/people/2
{
"firstName" : "Frodo",
"lastName" : "Baggins",
"_links" : {
"self" : {
"href" : "http://localhost:8080/people/2"
},
"person" : {
"href" : "http://localhost:8080/people/2"
}
}
}%
查看搜索方法:
curl http://localhost:8080/people/search
{
"_links" : {
"findByLastName" : {
"href" : "http://localhost:8080/people/search/findByLastName{?name}",
"templated" : true
},
"self" : {
"href" : "http://localhost:8080/people/search"
}
}
}%
使用搜索方法findByLastName
:
curl http://localhost:8080/people/search/findByLastName\?name\=Baggins
{
"_embedded" : {
"people" : [ {
"firstName" : "Frodo",
"lastName" : "Baggins",
"_links" : {
"self" : {
"href" : "http://localhost:8080/people/2"
},
"person" : {
"href" : "http://localhost:8080/people/2"
}
}
} ]
},
"_links" : {
"self" : {
"href" : "http://localhost:8080/people/search/findByLastName?name=Baggins"
}
}
}%
替换:
curl -X PUT -H "Content-Type:application/json" -d '{ "firstName": "Bilbo", "lastName": "Baggins" }' http://localhost:8080/people/2
{
"firstName" : "Bilbo",
"lastName" : "Baggins",
"_links" : {
"self" : {
"href" : "http://localhost:8080/people/2"
},
"person" : {
"href" : "http://localhost:8080/people/2"
}
}
}%
查看替换效果:
curl http://localhost:8080/people/2
{
"firstName" : "Bilbo",
"lastName" : "Baggins",
"_links" : {
"self" : {
"href" : "http://localhost:8080/people/2"
},
"person" : {
"href" : "http://localhost:8080/people/2"
}
}
}%
修改:
curl -X PATCH -H "Content-Type:application/json" -d '{ "firstName": "Bilbo Jr." }' http://localhost:8080/people/2
{
"firstName" : "Bilbo Jr.",
"lastName" : "Baggins",
"_links" : {
"self" : {
"href" : "http://localhost:8080/people/2"
},
"person" : {
"href" : "http://localhost:8080/people/2"
}
}
}%
删除:
curl -X DELETE http://localhost:8080/people/2
总结
这里主要介绍Neo4j在Spring Rest方式,但总觉得HAL标准有点喧宾夺主的味道。HAL是使用REST的一种标准方式,但是这个标准还在起草中的路上。
参考:
- Accessing Neo4j Data with REST
- neo4j的Hello World
- Gradle中lombok使用
- Hypertext Application Language