简体   繁体   English

春季REST HATEOAS

[英]Spring REST HATEOAS

I am trying to follow best practices imagine a basic API with /books endpoint. 我正在尝试遵循最佳实践,想象一个带有/ books端点的基本API。

I've the following classes: Book BookRepository (PagingAndSortingRepository but not exported!!) BookController (this is the serving class) BookResource ("representation" of the book in "REpresentational State Transfer") BookResourceAssembler 我有以下类:Book BookRepository(PagingAndSortingRepository,但未导出!)BookController(这是服务类)BookResource(“状态状态转移”中该书的“表示”)BookResourceAssembler

My BookController looks like this: 我的BookController看起来像这样:

@RequestMapping(path="/{id}", method = RequestMethod.GET)
    public ResponseEntity<?> getBook(@PathVariable Long id) {
        Book book = bookRepository.findOne(id);
        BookResource bookResource = this.bookResourceAssembler.toResource(book);
        return ResponseEntity.ok(bookResource);
    }

my BookResource looks like this: 我的BookResource看起来像这样:

public class BookResource extends ResourceSupport{
    public String title;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

}

BookResourceAssembler: BookResourceAssembler:

@Component
public class BookResourceAssembler extends ResourceAssemblerSupport<Book, BookResource> {

      public BookResourceAssembler() {
        super(BookController.class, BookResource.class);
      }


    @Override
    public BookResource toResource(Book entity) {
        BookResource resource = createResourceWithId(entity.getDbid(), entity);
        return resource;
    }
}

My first problem is easy one to solve I guess, title is not initialized so when I call /books/1 I get title: null. 我想我的第一个问题很容易解决,标题未初始化,因此当我调用/ books / 1时,标题为:null。

Second and more important question is how do I do content-negotiation and versioning, which is a really important aspect of RESTful API. 第二个也是更重要的问题是如何进行内容协商和版本控制,这是RESTful API的一个非常重要的方面。 How should I introduce BookResourceV2 and where should I negotiate it? 我应该如何介绍BookResourceV2,并在哪里进行谈判? Let's say I want 假设我要

"Content-Type: application/vnd.company.book+json.v2"

where do I state this? 我在哪里注明? Which new classes/functions should I add to handle this v2 of the same resource? 我应该添加哪些新的类/函数来处理同一资源的此v2?

I couldn't find a good tutorial that covers all aspects. 我找不到涵盖所有方面的优秀教程。

My first problem is easy one to solve I guess, title is not initialized so when I call /books/1 I get title: null. 我想我的第一个问题很容易解决,标题未初始化,因此当我调用/ books / 1时,标题为:null。

You're using BookResource the wrong way. 您使用BookResource的方式错误。 I assume your domain object (Book) looks something like: 我假设您的域对象(书)看起来像:

public class Book {
    public String title;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

}

BookResource is a wrapper that adds links to the domain object and present it in a RESTful way... BookResource是一个包装器,它添加到域对象的链接并以RESTful方式呈现。

public class BookResource extends Resource<Book> {

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

}

For the record, in this case you don't even need custom Resource and ResourceAssembler classes: 作为记录,在这种情况下,您甚至不需要自定义Resource和ResourceAssembler类:

@RequestMapping(path="/{id}", method = RequestMethod.GET)
public ResponseEntity getBook(@PathVariable Long id,
    PersistentEntityResourceAssembler assembler) {
    Book book = bookRepository.findOne(id);
    if (null == book) {
        throw new ResourceNotFoundException();
    }

    return ResponseEntity.ok(assembler.toResource(book));
}

In this very simple case (no special processing of the Book instance), you wouldn't even need this endpoint, since Spring generates it for you. 在这种非常简单的情况下(无需对Book实例进行特殊处理),您甚至不需要此端点,因为Spring会为您生成它。

Second and more important question is how do I do content-negotiation and versioning, which is a really important aspect of RESTful API. 第二个也是更重要的问题是如何进行内容协商和版本控制,这是RESTful API的一个非常重要的方面。 How should I introduce BookResourceV2 and where should I negotiate it? 我应该如何介绍BookResourceV2,并在哪里进行谈判?

You need to define a RepositoryRestController and use the produces parameter of your RequestMapping -annotated controller endpoints, similarly to what I explained in this post . 您需要定义一个RepositoryRestController并使用带有RequestMapping控制器端点的produces参数,类似于我在本文中所解释

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

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