I am doing a restful application.I have been trying to build a hypermedia link for a method which has pathvariable teacherId
as shown in my code below. I used methodOn
to build a link for getAllToturialByTeacher
method, for example, http://localhost:8080/springexample/teachers/2/toturials
@RestController
@RequestMapping( value = "/teachers", produces = { MediaType.APPLICATION_JSON_VALUE } )
public class ToturialController {
......
@RequestMapping( value = "/{teacherId}/toturials",method = RequestMethod.GET )
public ResponseEntity<Resources<Resource<Toturial>>> getAllToturialByTeacher(int teacherId){
......
resource.add(linkTo(ToturialController.class)
.slash(linkTo(methodOn(ToturialController.class).getAllToturialByTeacher(teacherId)))
.withRel("subjectsByTeacher"));
....
return new ResponseEntity<Resources<Resource<Toturial>>>(resource, HttpStatus.OK);
}
}
And I got exception error
java.lang.IllegalArgumentException: Map has no value for 'teacherId'
at org.springframework.web.util.UriComponents$MapTemplateVariables.getValue(UriComponents.java:306)
at org.springframework.web.util.UriComponents.expandUriComponent(UriComponents.java:230)
at org.springframework.web.util.HierarchicalUriComponents$FullPathComponent.expand(HierarchicalUriComponents.java:685)
at org.springframework.web.util.HierarchicalUriComponents.expandInternal(HierarchicalUriComponents.java:328)
at org.springframework.web.util.HierarchicalUriComponents.expandInternal(HierarchicalUriComponents.java:47)
at org.springframework.web.util.UriComponents.expand(UriComponents.java:152)
at org.springframework.web.util.UriComponentsBuilder.buildAndExpand(UriComponentsBuilder.java:398)
at org.springframework.hateoas.mvc.ControllerLinkBuilderFactory.linkTo(ControllerLinkBuilderFactory.java:139)
at org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo(ControllerLinkBuilder.java:138)
I could not set the value for placeholder {teacherId}
. Could anyone give me a solution or best way to handle this.