Spring HATEOAS链接矩阵变量不起作用

时间:2016-01-18 08:33:59

标签: spring-data spring-data-rest spring-hateoas

我正在尝试从Spring MVC控制器生成并添加指向RESTful资源的链接。我们的API需要使用HTTP矩阵变量。不幸的是,生成的自身链接缺少URI中的矩阵变量。

@BasePathAwareController
@RequestMapping("/licenses")
public class LicenseController {

  @Autowired
  private LicenseRepository repository;

  @RequestMapping(path = "/{licenseId}/violations", method = RequestMethod.GET)
  @RestResource(rel = "violations")
  @ResponseBody
  @Transactional(readOnly = true)
  public ResponseEntity<?> getViolations(@PathVariable String licenseId, @MatrixVariable(name = "state") String state) {
    try {
      StateContextHolder.setState(state);
      List<ViolationEntity> violations = repository.findOne(licenseId).getViolations();

      if (violations == null) {
        return new ResponseEntity<Resources<?>>(HttpStatus.OK);
      }
      else {
        Resources<?> entityResource = new Resources(violations);
        entityResource.add(linkTo(methodOn(LicenseController.class).getViolations(licenseId, state)).withSelfRel());

        return new ResponseEntity<Resources<?>>(entityResource, HttpStatus.OK);
      }
    }
    finally {
      StateContextHolder.clear();
    }

  }
}

对于HTTP GET / licenses / 123456789; state = NY / violation,返回的自链接缺少状态矩阵参数:

{
  ...,
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/licenses/123456789/violations"
    }
  }
}

我不想对此进行硬编码,所以我试图弄清楚如何使用Spring HATEOAS或Spring Data REST API来实现这一点。

我需要使用矩阵参数来优化许可证路径元素。您可以详细了解here的原因。可以这么说,在Spring Data从存储库中检索实体之前,我们正在做一些自定义行为。

1 个答案:

答案 0 :(得分:0)

我认为Spring HATEOAS目前不支持矩阵参数。

  1. 请参阅UriTemplate的这一部分。它使用不同的变量类型来构建Uri。矩阵参数不是其中之一。
  2. 请参阅用于启用的VariableType枚举。也没有矩阵参数。