简体   繁体   English

实施Spring HATEOAS链接构建

[英]Implementing Spring HATEOAS linkbuilding

I don't understand what I need to add to make my program become HATEOAS. 我不明白我需要添加什么才能使我的程序成为HATEOAS。 I have Account .java, Post .java, Controllers and repositories . 我有Account .java, Post .java, Controllersrepository From some guides they add AccountResource and PostResource where they build links inside these classes. 他们从一些指南中添加AccountResource和PostResource,以便在这些类中构建链接。 What is the difference between AccountResource and Account? AccountResource和Account有什么区别? Do I need both? 我两个都需要吗? If so, do I create a resource class for each normal class? 如果是这样,我是否为每个普通类创建一个资源类? I tried doing this and it didn't work at all. 我尝试这样做,但根本没有用。 I have no idea what I'm doing anymore :( . I need some help understanding how to migrate from a normal REST to HATEOAS. What classes do I need to add? 我不知道我正在做什么:(。我需要一些帮助,以了解如何从常规REST迁移到HATEOAS。我需要添加哪些类?

public class Account {

//Account ID
@Id private String userId;

//General info
protected String firstName;
protected String lastName;
protected String username;
protected String email;
protected String password;
protected String birthDate;
protected String activities;
protected String uri;
private Set<Post> posts = new HashSet<>();
List<Account> friends = new ArrayList<Account>();

//Getter, constructor...



@RestController
public class AccountController {    

@Autowired
private AccountRepository accountRepository;

//Create account 
@RequestMapping(value="/accounts", method = RequestMethod.POST) 
public ResponseEntity<?> accountInsert(@RequestBody Account account) {

    account = new Account(account.getUri(), account.getUsername(), account.getFirstName(), account.getLastName(), account.getEmail(), account.getPassword(), account.getBirthDate(), account.getActivities(), account.getFriends());
    accountRepository.save(account);
    HttpHeaders httpHeaders = new HttpHeaders();
    Link forOneAccount = new AccountResource(account).getLink("self");
    httpHeaders.setLocation(URI.create(forOneAccount.getHref()));

    return new ResponseEntity<>(null, httpHeaders, HttpStatus.CREATED);
}



public class AccountResource extends ResourceSupport {

private Account account;

public AccountResource(Account account) {
    String username = account.getUsername();
    this.account = account;
    this.add(new Link(account.getUri(), "account-uri"));
    this.add(linkTo(AccountController.class, username).withRel("accounts"));
    this.add(linkTo(methodOn(AccountController.class, username).getUniqueAccount(account.getUserId())).withSelfRel());
}

public AccountResource() {

}

public Account getAccount() {
    return account;
}
}

Account , Post etc are your domain entities, while the *Resource are their representation exposed by your API to the external world. AccountPost等是您的域实体,而*Resource是您的API向外界公开的表示。

Domain entities might be, for example, JPA Entities, containing all the metadata required for their persistency and relations with other entities. 域实体可能是例如JPA实体,包含它们的持久性以及与其他实体的关系所需的所有元数据。 Even if they are not JPA entities, they are the internal representation used by your application business logic. 即使它们不是JPA实体,它们也是您的应用程序业务逻辑使用的内部表示形式。

Resources contains the information for JSON Serialisation/Deserialisation, and no direct references to other resources. 资源包含有关JSON序列化/反序列化的信息,而没有直接引用其他资源。

Have a look at this sample project https://github.com/opencredo/spring-hateoas-sample and to the related blog post: Implementing HAL hypermedia REST API using Spring HATEOAS 看看这个示例项目https://github.com/opencredo/spring-hateoas-sample和相关博客文章: 使用Spring HATEOAS实现HAL超媒体REST API

It implements a simple, but not-so-trivial library API, with a Book - Author - Publisher domain. 它使用Book-Author-Publisher域实现了一个简单但不那么简单的库API。

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

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