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.