Java Spring REST控制器保存JSON Post
我被什么东西卡住了,经过一天的搜索和尝试,我终于认输了。我有两个基本域名,一个博客帖子和一个作者。为了使这篇文章简短,我省略了一些代码Java Spring REST控制器保存JSON Post,java,json,spring,rest,Java,Json,Spring,Rest,我被什么东西卡住了,经过一天的搜索和尝试,我终于认输了。我有两个基本域名,一个博客帖子和一个作者。为了使这篇文章简短,我省略了一些代码 @Entity public class Post { @Id @GeneratedValue private Long id; private String title; @Column(columnDefinition = "TEXT") private String body; @Column(colu
@Entity
public class Post {
@Id @GeneratedValue
private Long id;
private String title;
@Column(columnDefinition = "TEXT")
private String body;
@Column(columnDefinition = "TEXT")
private String teaser;
private String slug;
@CreatedDate
@Temporal(TemporalType.TIMESTAMP)
private Date postedOn;
@ManyToOne
private Author author;
// getters & setters
}
@Entity
public class Author {
@Id
@GeneratedValue
private Long id;
private String firstName;
private String lastName;
private String email;
// getters & setters
}
控制器看起来像这样
@RestController
@RequestMapping("/posts")
public class PostController {
private PostService postService;
@Autowired
public PostController(PostServiceImpl postService){
this.postService = postService;
}
@RequestMapping( value = "/", method = RequestMethod.GET )
public Iterable<Post> list(){
return postService.list();
}
@RequestMapping( value = "/", method = RequestMethod.POST )
public Post create(@RequestBody Post post){
return postService.save(post);
}
@RequestMapping( value = "/{id}", method = RequestMethod.GET )
public Post read(@PathVariable(value="id") long id){
return postService.getPost(id);
}
@RequestMapping( value = "/{id}", method = RequestMethod.PUT )
public String update(@PathVariable(value="id") int id){
return "post.update()";
}
@RequestMapping( value = "/{id}", method = RequestMethod.DELETE )
public String delete(@PathVariable(value="id") int id){
return "post.delete()";
}
}
当我尝试发布这个JSON时
{
"title" : "A new post created from JSON",
"slug" : "a-new-post",
"teaser" : "post teaser",
"body" : "post body",
"postedOn" : "2015-11-07",
"author" : {
"firstName": "Joe",
"lastName": "Smith",
"email": "jsmith@gmail.com"
}
}
我得到以下错误
{
"timestamp": 1447018768572,
"status": 500,
"error": "Internal Server Error",
"exception": "org.springframework.dao.InvalidDataAccessApiUsageException",
"message": "org.hibernate.TransientPropertyValueException: object references an unsaved transient instance - save the transient instance before flushing : com.therealdanvega.domain.Post.author -> com.therealdanvega.domain.Author; nested exception is java.lang.IllegalStateException: org.hibernate.TransientPropertyValueException: object references an unsaved transient instance - save the transient instance before flushing : com.therealdanvega.domain.Post.author -> com.therealdanvega.domain.Author",
"path": "/posts/"
}
您首先需要坚持作者。从Post的角度来看,您呈现给它的作者是暂时的,因为它没有id,因此无法在Post和作者之间进行映射
因此,创建作者的持久对象,然后将其插入Post实体。因此,我更新了我的PostService save方法,首先调用authorService.save(Post.Author),结果似乎很好。谢谢我只是好奇,如果我已经知道作者是谁,我会通过什么?也许还有身份证?作者:{“id”:1}我想说的是按姓名或id查询作者,然后使用setter将其附加到帖子上。一个简单的ID也应该足够了,但您可能会遇到同样的问题。因此,在我看来,这将是类似于
Post Post=new Post()代码>post.setAuthor(authorService.get(authorId))
//将其余信息设置为post
/postService.save(post)代码>
{
"timestamp": 1447018768572,
"status": 500,
"error": "Internal Server Error",
"exception": "org.springframework.dao.InvalidDataAccessApiUsageException",
"message": "org.hibernate.TransientPropertyValueException: object references an unsaved transient instance - save the transient instance before flushing : com.therealdanvega.domain.Post.author -> com.therealdanvega.domain.Author; nested exception is java.lang.IllegalStateException: org.hibernate.TransientPropertyValueException: object references an unsaved transient instance - save the transient instance before flushing : com.therealdanvega.domain.Post.author -> com.therealdanvega.domain.Author",
"path": "/posts/"
}