14

現在、次のように @RepositoryRestResource で注釈を付けることで、いくつかの Spring Data Repositories を RESTful サービスとして公開しています。

@RepositoryRestResource(collectionResourceRel = "thing1", path = "thing1")
public interface Thing1Repository extends PagingAndSortingRepository<Thing1, String> {}

@RepositoryRestResource(collectionResourceRel = "thing2", path = "thing2")
public interface Thing2Repository extends CrudRepository<Thing2, String> {}

これはすべてうまくいきます。最初のエンドポイントに到達すると、次のように、公開したすべての Spring Data リポジトリも表示されます。

{
   _links: {
      thing1: {
         href: "http://localhost:8080/thing1{?page,size,sort}",
         templated: true
      },
      thing2: {
         href: "http://localhost:8080/thing2"
      }
   }
}

Spring Data Repositories では表現できない、公開したいエンドポイントがいくつかあるので、RestController を使用しています。

以下に簡単な例を示します。

@RestController
@ExposesResourceFor(Thing3.class)
@RequestMapping("/thing3")
public class Thing3Controller {

  @Autowired 
  EntityLinks entityLinks;

  @Autowired 
  Thing3DAO thing3DAO;

  //just assume Thing3.class extends ResourceSupport. I know this is wrong, but it makes the example shorter  
  @RequestMapping(value = "/{id}", produces = "application/json")
  Thing3 thing3(@PathVariable("id") String id)
  {
      Thing3 thing3 = thing3DAO.findOne(id);         

      Link link = entityLinks.linkToSingleResource(Thing3.class, id);
      thing3.add(link);

      return thing3;
  }
}

このアプリを実行して次の場所に移動すると:

http://localhost:8080/thing3/{id} 

それ自体へのリンクを含む Thing3 の JSON 表現を取得しますが、これは期待どおりに機能します。

どうすればよいか理解したいのは、最初のエンドポイントにもこのコントローラーを記述させることです。私は基本的にこれが欲しい:

{
   _links: {
      thing1: {
         href: "http://localhost:8080/thing1{?page,size,sort}",
         templated: true
      },
      thing2: {
         href: "http://localhost:8080/thing2"
      },
      thing3: {
         href: "http://localhost:8080/thing3"
      }
   }
}

ベース エンドポイントにこのコントローラーへのリンクを設定するには、どうすればよいですか?

4

1 に答える 1