0

I am trying to generate and add a link to a RESTful resource from a Spring MVC controller. Our API requires the use of HTTP matrix variables. Unfortunately, the self link generated is missing the matrix variable from the 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();
    }

  }
}

For the HTTP GET /licenses/123456789;state=NY/violations, the returned self link is missing the state matrix parameter:

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

I don't want to hardcode this in, so I am trying to figure out how to do this with the Spring HATEOAS or Spring Data REST APIs.

I need to use matrix parameters in order to refine the licenses path element. You can read more about why here. Suffice it to say, there is some custom behavior we are doing prior to Spring Data retrieving the entities from the repository.

Community
  • 1
  • 1
  • _"I need to use matrix parameters in order to refine the licenses path element"_ - Assuming there is a license resource and license IDs are not unique across the states then `123456789;state=NY` as a whole is a key. There is no refinement. FWIW you can try `/{licenseId};state={state}/violations` with `state` being a path variable. – a better oliver Jan 21 '16 at 18:18

1 Answers1

0

As I see it Spring HATEOAS has no support for matrix parameters at this time.

  1. See this part of the UriTemplate. It uses different variable types to build the Uri. A matrix parameter isn't one of them.
  2. See the VariableType enum, which was used to do the switch on. There is also no matrix param.
Dennis Stritzke
  • 5,198
  • 1
  • 19
  • 28