Json 使用Spring Boot和Neo4j通过REST API插入实体时出错
我正在用Spring Boot测试Neo4j,当我尝试使用Rest API插入元素时,我发现了以下问题:JSON无法序列化为实体。如果有人能帮助我或解释如何更改我的代码或如何序列化该实体,我将非常感激。 我的课程是。。。 用户实体Json 使用Spring Boot和Neo4j通过REST API插入实体时出错,json,spring,spring-boot,neo4j,spring-data-neo4j-5,Json,Spring,Spring Boot,Neo4j,Spring Data Neo4j 5,我正在用Spring Boot测试Neo4j,当我尝试使用Rest API插入元素时,我发现了以下问题:JSON无法序列化为实体。如果有人能帮助我或解释如何更改我的代码或如何序列化该实体,我将非常感激。 我的课程是。。。 用户实体 @NodeEntity public class User extends AbstractEntity{ @Id @GeneratedValue private Long id; private String firstname,
@NodeEntity
public class User extends AbstractEntity{
@Id
@GeneratedValue
private Long id;
private String firstname, lastname;
@NotNull @NotBlank @Email
@Index(unique = true)
private String email;
@Index(unique = true)
private String phone;
@Relationship(type = "ADDRESS")
private Set<Address> addresses = new HashSet<>();
public User() {
}
//get & set & const....
}
国家实体
@NodeEntity
public class Country extends AbstractEntity {
/*Update the OMG [https://neo4j.com/developer/neo4j-ogm] and remove de id.
Keep the id in AbstractEntity*/
@Id
@GeneratedValue
private Long id;
@Index(unique=true)
private String code;
private String name;
public Country(String code, String name) {
this.code = code;
this.name = name;
}
public Country() {
}
}
抽象实体
@EnableNeo4jAuditing
public abstract class AbstractEntity {
public abstract Long getId();
@Transient
private SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");
private Date created = new Date();
public Date getCreated() {
return created;
}
public void setCreated(Date created) {
this.created = created;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (getId() == null || obj == null || !getClass().equals(obj.getClass())) {
return false;
}
return getId().equals(((AbstractEntity) obj).getId());
}
@Override
public int hashCode() {
return getId() == null ? 0 : getId().hashCode();
}
}
我的商店用户的简单存储库类
public interface UserRepository extends Neo4jRepository<User, Long> {
User findByFirstname(String name);
@Override
void delete(User deleted);
}
如您所见,国家/地区将其作为空值返回,如果它存在于数据库中
当我试图使用API插入一个元素时,它哀叹了一个错误,我无法将country对象序列化为JSON
$ curl -i -X POST -H "Content-Type: application/json" -d '{"firstname":"Yuniel","lastname":"Acosta Pérez","email":"yuniel.acosta@someserver.com","phone":"+999999999999","addresses":[{"street":"Some Stree","city":"Some City","country":[{"code":"OO","name":"ANYCOUNTRY"}]}]}' localhost:8088/users
下一个错误就是把我扔出去的那个
{“时间戳”:“2018-09-27T21:07:56.365+0000”,“状态”:400,“错误”:“错误请求”,“消息”:“JSON解析错误:无法反序列化com.syskayzen.hypercube.domain.Address
out-of-START\u数组标记的实例;嵌套异常为com.fasterxml.jackson.databind.exc.MismatchedInputException:无法反序列化com.syskayzen.hypercube.domain.Address
out-of-of-START\u数组标记的实例,位于[源代码:(PushbackInputStream);行:1,列:122](通过引用链:com.syskayzen.hypercube.domain.User[\“addresses\”]),“path:“/users”}
如果您能告诉我如何解决如何序列化国家/地区实体的问题,或者是由于其他原因导致了错误
我使用的是Spring Boot 2.0.5和Spring Data Neo4j,我相信在curl命令中Country返回null的原因是因为查询在数据库中只深入了1跳。因此,它会拉用户,然后再拉地址节点1跳,但随后会停止。要让它沿着路径拉更多跳,需要指定一个depth 您可以通过在curl命令中指定一个深度,并为“int depth”添加一个参数来传递存储库调用来实现这一点。这将允许您拥有一个动态深度,以防您向要检索的应用程序添加更多跃点 服务方法看起来像这样
public User show(Long id, int depth) {
return userRepository.findById(id, depth).get();
}
以下链接提供了相关文档:
@RestController
public class UserController {
@Autowired
UserService userService;
@RequestMapping(method = RequestMethod.GET, value = "/users")
public Iterable<User> user() {
return userService.contact();
}
@RequestMapping(method = RequestMethod.POST, value = "/users")
public String save(@RequestBody User user) {
try {
userService.save(user);
}catch (org.neo4j.driver.v1.exceptions.ClientException ex){
System.err.println("******||||||||||||[{ The User exist with the same email }]||||||||||||******");
return "The User exist with the same email";
}
return user.toString();
}
@RequestMapping(method=RequestMethod.GET, value="/users/{id}")
public User show(@PathVariable Long id) {
try{
return userService.show(id);}
catch (java.util.NoSuchElementException ex){
System.err.println("******||||||||||||[{ The User do not exist }]||||||||||||******");
}
return null;
}
@RequestMapping(method=RequestMethod.PUT, value="/users/{id}")
public User update(@PathVariable Long id, @RequestBody User user) {
return userService.update(id, user);
}
@RequestMapping(method=RequestMethod.DELETE, value="/users/{id}")
public String delete(@PathVariable Long id) {
return userService.delete(id);
}
}
$ curl -i -H "Accept: application/json" localhost:8088/users/1
HTTP/1.1 200
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Thu, 27 Sep 2018 20:38:31 GMT
{"created":"2018-09-27T19:55:21.578+0000","id":1,"firstname":"Yuniel","lastname":"Acosta Pérez","email":"yuniel.acosta@someserver.com","phone":"+999999999999","addresses":[{"created":"2018-09-27T19:55:21.578+0000","id":0,"street":"Some Stree","city":"Some City","country":null}]}
$ curl -i -X POST -H "Content-Type: application/json" -d '{"firstname":"Yuniel","lastname":"Acosta Pérez","email":"yuniel.acosta@someserver.com","phone":"+999999999999","addresses":[{"street":"Some Stree","city":"Some City","country":[{"code":"OO","name":"ANYCOUNTRY"}]}]}' localhost:8088/users
public User show(Long id, int depth) {
return userRepository.findById(id, depth).get();
}