-
Neo4j RestAPI 서버 구성하기 #1Intelligent Product/Neo4J 2022. 5. 9. 23:51
neo4j Person 객체 다음 가이드를 참고하여 Neo4J의 데이터를 읽고 쓸 수 있는 Rest API 서버를 구성하였다.
https://spring.io/guides/gs/accessing-data-neo4j/
Accessing Data with Neo4j
this guide is designed to get you productive as quickly as possible and using the latest Spring project releases and techniques as recommended by the Spring team
spring.io
구성한 서버는 Neo4J 서버에서 Person 객체 정보를 조회하여 반환해준다.
springboot initializr 에서 neo4j를 추가하여 프로젝트를 생성한다.(REST API 구성을 위해 spring-boot-starter-web도 추가한다.) https://start.spring.io/
spring initializr 구성한 프로젝트의 구조는 다음과 같다.
프로젝트 구조 Person.java (Person 노드 모델)
@Getter @Setter @Node public class Person { @Id @GeneratedValue private long id; private String name; @Relationship(type = "IS_FRIEND_OF") public Set<Person> friends = new HashSet<>(); }
PersonRepository.java (JPA와 같이 쿼리를 자동생성 해준다.)
import com.foodraph.model.Person; import org.springframework.data.neo4j.repository.Neo4jRepository; public interface PersonRepository extends Neo4jRepository<Person, Long> { List<Person> findAll(); Person findByName(String name); Set<Person> findFriendsByName(String name); }
PersonController.java
@RestController @RequiredArgsConstructor public class PersonController { private static final String MESSAGE = "Hello, World!"; private final PersonRepository personRepository; @GetMapping(path ="/v1/persons", produces = {MediaType.APPLICATION_JSON_VALUE}) public List<Person> findPerson() { List<Person> list = personRepository.findAll(); return list; } @GetMapping(path ="/v1/person/{name}", produces = {MediaType.APPLICATION_JSON_VALUE}) public Person findPersonByName(final @PathVariable(name = "name") String name) { return personRepository.findByName(name); } @GetMapping(path ="/v1/person/friends/{name}", produces = {MediaType.APPLICATION_JSON_VALUE}) public Set<Person> findFriends(final @PathVariable(name = "name") String name) { Set<Person> list = personRepository.findFriendsByName(name); return list; } }
데이터 요청 결과
1. Person 전체 데이터 요청
2. name을 이용한 검색
3. 특정 Person의 친구를 조회(Alice의 친구를 검색)
'Intelligent Product > Neo4J' 카테고리의 다른 글
Neo4j Rest API 서버 구성하기 #3 (0) 2022.05.11 Neo4j RestAPI 서버 구성하기 #2 (0) 2022.05.10 neo4j example - 레스토랑 (0) 2022.03.03 Neo4J 데이터 셋 구성 (0) 2022.03.03 Neo4J 세팅 (0) 2022.03.03