[英]REST API with Spring Boot and Spring Data Neo4j
我正在尝试使用spring-boot作为后端,将Angular作为前端,以及作为Neo4j数据库(面向图形的NoSQL数据库)的应用程序。 我的问题是我不知道如何在NodeEntity中使用复杂的数据。 我正在编程的应用程序将基于微服务架构,其中Angular(前端)中的应用程序将通过spring-boot制成的后端的Rest API插入和使用数据,该数据将存储在服务器中。从Neo4j数据库运行实例。 这是我在做什么的一个例子。 这是application.properties文件
spring.data.neo4j.uri =螺栓://本地主机spring.data.neo4j。用户名= neo4j spring.data.neo4j。密码=秘密
这是我的域模型示例类:
@NodeEntity
public class Contact {
@Id
Long id;
String name;
String address;
String city;
String phone;
String email;
public Contact(String name, String address, String city, String phone, String email) {
this.name = name;
this.address = address;
this.city = city;
this.phone = phone;
this.email = email;
}
//get and set
}
如您所见,是一个具有不同属性的简单类,但是这些属性从本质上讲可以是更复杂的数据,例如地址,它本身可以是由属性,城市,街道,国家,#,...组成的NodeEntity。
我的存储库将像这样简单:
public interface ContactRepository extends Neo4jRepository<Contact,Long>{
Collection<Contact> findByName(String name);
@Override
void delete(Contact deleted);
}
ContactService类将负责处理ContactRepository
@Service
public class ContactService {
@Autowired
private final ContactRepository contactRepository;
public ContactService(ContactRepository contactRepository) {
this.contactRepository = contactRepository;
}
@Transactional(readOnly = true)
public Iterable<Contact> contact() {
return contactRepository.findAll();
}
@Transactional(readOnly = true)
public Contact show(Long id) {
return contactRepository.findById(id).get();
}
public Contact save(Contact contact) {
contactRepository.save(contact);
return contact;
}
public Contact update(Long id, Contact contact) {
Contact c = contactRepository.findById(id).get();
if (contact.getName() != null)
c.setName(contact.getName());
if (contact.getAddress() != null)
c.setAddress(contact.getAddress());
if (contact.getPhone() != null)
c.setPhone(contact.getPhone());
if (contact.getEmail() != null)
c.setEmail(contact.getEmail());
contactRepository.save(c);
return contact;
}
public String delete(Long id) {
Contact contact = contactRepository.findById(id).get() ;
contactRepository.delete(contact);
return "";
}
}
最后,负责Rest API的控制器
@RestController
public class ContactController {
@Autowired
private ContactService contactService;
@RequestMapping(method = RequestMethod.GET,value = "/contacts")
public Iterable<Contact> contact(){
return contactService.contact();
}
@RequestMapping(method = RequestMethod.POST,value = "/contacts")
public Contact save(@RequestBody Contact contact){
return contactService.save(contact);
}
@RequestMapping(method = RequestMethod.GET,value = "/contacts/{id}")
public Contact show(@PathVariable Long id){
return contactService.show(id);
}
@RequestMapping(method = RequestMethod.PUT,value = "/contacts/{id}")
public Contact update(@PathVariable Long id,@RequestBody Contact contact){
return contactService.update(id,contact);
}
@RequestMapping(method = RequestMethod.DELETE,value = "/contacts/{id}")
public String delete(@PathVariable Long id){
contactService.delete(id);
return "";
}
}
要测试API的工作方式,请使用以下命令
curl -i -H "Accept: application/json" localhost:8080/contacts
结果
HTTP / 1.1 200内容类型:application / json; charset = UTF-8传输编码:分块日期:2018年7月5日星期一12:14:39 GMT []
在数据库中插入新联系人
curl -i -X POST -H "Content-Type: application/json" -d '{"name":"John Doe","address":"Jl. Cipaganti No.112","city":"Bandung","phone":"0811223112233","email":"john.doe@dummy.com"}' localhost:8080/contacts
结果返回
HTTP / 1.1 200内容类型:application / json; charset = UTF-8传输编码:分块日期:2018年7月6日星期二02:59:00 GMT {“ id”:“ 5a7919f4b9d4f13c2cb6f539”,“名称”:“ John Doe”,“地址”:“ Ji。Cipaganti No.112”,“ city”:“ Bandung”,“ phone”:“ 0811223112233”,“ email”:“ john.doe@dummy.com”}
当我尝试更改NodeEntity类的address属性时,会出现问题,因为我已经看过许多教程,但是仅如何使用Rest API并没有提供如何插入数据。 如何向联系人类添加地址属性,该属性是表示数据库中节点(NodeEntity)的类? 我如何执行curl命令来证明api正常工作,因为现在address属性将是具有多个属性的类,例如city,street,country,#,...?
希望您能帮助我,并感谢您的宝贵时间。
@NodeEntity
public class Contact {
@Id
Long id;
String name;
@Relationship(type = HAS_ADDRESS)
Address address;
}
just create a relationship between 2 nodes and save as an embeded node.
您可以创建与上述节点类似的地址节点
@NodeEntity
public class Address{
// properties and getter setters.
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.