在春天HATEOAS的居里夫妇

时间:2015-03-03 11:13:18

标签: rest debugging spring-boot spring-hateoas curie

我使用Spring Boot和HATEOAS来构建REST API,我正在努力创建curie。 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部分添加一个链接,则会显示居里链接:

{
    "_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;
    }

}

1 个答案:

答案 0 :(得分:0)

您的配置类对我来说是正确的,但是Spring Boot的HypermediaAutoConfiguration需要org.springframework.plugin:spring-plugin-core(Spring HATEOAS的可选依赖项)才能在类路径上启用它。我猜你错过了这种依赖。尝试在org.springframework.plugin:spring-plugin-core:1.1.0.RELEASE上添加依赖项。