【问题标题】:Spring REST Controller Saving JSON PostSpring REST 控制器保存 JSON 帖子
【发布时间】:2015-11-08 21:41:11
【问题描述】:

我被一些事情困住了,经过一天的搜索和尝试不同的事情,我认输了。我有 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 并调用存储库上的保存方法。这是我的问题,我什至觉得问它很愚蠢。当我从没有作者(null)的 Postman 发布 JSON 时,一切正常。我只是不确定如何用新作者或现有作者发布 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/"
}

【问题讨论】:

    标签: java json spring rest


    【解决方案1】:

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

    所以创建一个作者的持久对象,然后将其插入到 Post 实体中。

    【讨论】:

    • 所以我更新了我的 PostService 保存方法以首先调用 authorService.save(post.author) ,这似乎很好。谢谢。只是好奇在我已经知道作者是谁的情况下我会通过什么?也许和身份证?作者:{“id”:1 }
    • 好吧,我会说通过姓名或 ID 查询作者,然后使用您的设置器将其附加到帖子。一个简单的 ID 也应该足够了,但您可能会遇到同样的问题。所以在我的脑海里,它会像Post post = new Post(); post.setAuthor(authorService.get(authorId)); // 将其余信息设置为post // postService.save(post);
    猜你喜欢
    • 1970-01-01
    • 2016-07-06
    • 2018-10-04
    • 1970-01-01
    • 2017-01-07
    • 2019-06-06
    • 1970-01-01
    • 2018-09-12
    • 2019-07-21
    相关资源
    最近更新 更多