0

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.

Khaino
  • 3,774
  • 1
  • 27
  • 36

1 Answers1

2

In your controller the teacherId is not annotated as @PathVariable.

Also your link creation looks broken - try this:

resource.add(linkTo(methodOn(ToturialController.class).getAllToturialByTeacher(teacherId)).withRel("subjectsByTeacher"))
Mathias Dpunkt
  • 11,594
  • 4
  • 45
  • 70
  • I think this is the same as mine except for number of lines which I broke into 3 lines for more `readability`. Thanks for your answer. – Khaino Nov 09 '15 at 08:49
  • No it is not - you have this `.slash()` method first - that is useless. Also the main point is that you do not annotate your controller method argument with `@PathVariable` - that is your main problem. – Mathias Dpunkt Nov 09 '15 at 08:58
  • Yes, you are right. I tried your code but did not work. Thanks – Khaino Nov 09 '15 at 09:10
  • What did not work? Did the @PathVariable solve your issue? – Mathias Dpunkt Nov 09 '15 at 09:13