Spring REST控制器保存JSON帖子

时间:2015-11-08 21:41:12

标签: java json spring rest

我被困在一些东西上,经过一天的搜索和尝试不同的事情,我正在抛弃。我有2个基本域名,一篇博客文章和一篇作者。我留下了一些代码来保持这篇文章的简短。

@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()";
    }


}

所有服务方法都采用Post POJO并在存储库中调用save方法。这是我的问题,即使提出这个问题,我也感到愚蠢。当我从Postman发布没有作者的JSON(null)时,一切正常。我只是不确定如何用新作者或现有作者发布json对象。

这有效

{
    "title" : "A new post created from JSON",
    "slug" : "a-new-post",
    "teaser" : "post teaser",
    "body" : "post body",
    "postedOn" : "2015-11-07"
}

当我尝试发布此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/"
}

1 个答案:

答案 0 :(得分:0)

首先需要坚持作者。从Post的角度来看,你呈现给它的作者是Transient,因为它没有id,因此Post和Author之间不能进行映射。

因此,创建Author的持久对象,然后将其插入Post实体。