简体   繁体   English

Spring HATEOAS resourcesupport - >域对象

[英]Spring HATEOAS resourcesupport -> domain object

While serving objects we use toResource method to transform them into resources and on the way back (posting a resource representation from client to server) how can I transform the representation back to the domain object? 在服务对象时,我们使用toResource方法将它们转换为资源并在返回的路上(将资源表示从客户端发布到服务器)如​​何将表示转换回域对象?

I want to construct Book(@Entity) class from BookResource(extends ResourceSupport) class. 我想从BookResource(扩展ResourceSupport)类构造Book(@Entity)类。

@RequestMapping(path="/", method = RequestMethod.POST, produces="application/vnd.company.app.book-v1+hal+json")
    public ResponseEntity<?> addBook(@RequestBody BookResource bookResource) {
        //What to do here?
    }

Your BookResource should extend Resource rather than ResourceSupport. 您的BookResource应该扩展Resource而不是ResourceSupport。

public class BookResource extends Resource<Book> {

    public BookResource(Book content, Link... links) {
        super(content, links);
    }

}

That way, you get the getContent() method for free, which "Returns the underlying entity." 这样,你可以免费获得getContent()方法,“返回底层实体”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM