2016-04-27 9 views
0

プログラムをHATEOASにするために追加する必要があることを理解できません。私はアカウント .java、ポスト .java、コントローラーレポジトリを持っています。いくつかのガイドでは、AccountResourceとPostResourceを追加して、これらのクラス内のリンクを作成します。 AccountResourceとAccountの違いは何ですか?私は両方が必要ですか?その場合は、通常のクラスごとにリソースクラスを作成しますか?私はこれをやってみたが、全く動かなかった。私は:(もうやっている見当がつかない。私はHATEOASに、通常のRESTから移行する方法を理解するいくつかの助けを必要としています。私は追加する必要がありますどのようなクラス?Spring 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; 
} 
} 

答えて

0

AccountPostなどがあなたのドメインです*ResourceはあなたのAPIによって外部の世界に公開されています

ドメインエンティティは、永続性と他のエンティティとの関係に必要なすべてのメタデータを含むJPAエンティティなどです。 JPAエンティティではないため、アプリケーションビジネスロジックで使用される内部表現です。

リソースには、JSONシリアライゼーション/デシリアライゼーションに関する情報が含まれており、他のリソースへの直接参照は含まれていません。

は、このサンプルプロジェクトhttps://github.com/opencredo/spring-hateoas-sampleを見てください、関連するブログ記事へ:Implementing HAL hypermedia REST API using Spring HATEOAS

それはブックで、シンプルですが、それほど簡単なことではないライブラリのAPIを実装 - 著者 - 出版社ドメイン。

関連する問題