Spring Boot と HATEOAS を使用して REST API を構築していますが、キュリーの作成に苦労しています。Spring HATEOAS ガイドによると、応答にキュリーを自動的に挿入するには、次のことを行う必要があります。
@Configuration
@EnableWebMvc
@EnableHypermediaSupport(type= {HypermediaType.HAL})
public class Config {
@Bean
public CurieProvider curieProvider() {
return new DefaultCurieProvider("ex", new UriTemplate("http://www.example.com{#rel}"));
}
}
私の構成クラスは次のようなものです:
@SpringBootApplication
public class ApiApplication {
public static void main(String[] args) {
SpringApplication.run(ApiApplication.class, args);
}
@Bean
public CurieProvider curieProvider() {
return new DefaultCurieProvider("xpto", new UriTemplate("http://www.xpto.com{#rel}"));
}
}
@EnableWebMvc を構成クラスに追加しようとしましたが、応答 (hal) のレンダリングが変更され、キュリーが表示されません。キュリーを作成するには、コントローラーで何かをする必要がありますか?
更新: Spring Hateoas を (0.17.0.RELEASE に) 更新したところ、コレクション名がキュリーでレンダリングされるようになりましたが、キュリーが _links セクションに表示されません。
{
"_links": {
"self": {
"href": "http://localhost:8080/technologies"
}
},
"_embedded": {
"mycurie:technology": [
{
"id": 1,
"description": "A",
"_links": {
"self": {
"href": "http://localhost:8080/technologies/1"
}
}
},
{
"id": 2,
"description": "B",
"_links": {
"self": {
"href": "http://localhost:8080/technologies/2"
}
}
}
]
}
}
_links セクションにリンクを 1 つ追加すると、キュリー リンクが表示されます。
{
"_links": {
"self": {
"href": "http://localhost:8080/technologies"
},
"mycurie:xpto": {
"href": "http://localhost:8080/xpto"
},
"curies": [
{
"href": "http://localhost:8080/rels/{rel}",
"name": "mycurie",
"templated": true
}
]
},
"_embedded": {
"mycurie:technology": [
{
"id": 1,
"description": "A",
"_links": {
"self": {
"href": "http://localhost:8080/technologies/1"
}
}
},
{
"id": 2,
"description": "B",
"_links": {
"self": {
"href": "http://localhost:8080/technologies/2"
}
}
}
]
}
}
これは私のコントローラーです:
@RestController
@ExposesResourceFor(Technology.class)
@RequestMapping(value = "/technologies")
public class TechnologyRestController {
...
@RequestMapping(method = RequestMethod.GET, produces = "application/vnd.xpto-technologies.text+json")
public Resources<TechnologyResource> getAllTechnologies() {
List<Technology> technologies = technologyGateway.getAllTechnologies();
Resources<TechnologyResource> technologiesResources = new Resources<TechnologyResource>(technologyResourceAssembler.toResources(technologies));
technologiesResources.add(linkTo(methodOn(TechnologyRestController.class).getAllTechnologies()).withSelfRel());
return technologiesResources;
}
}