我正在做一个restful应用程序,我一直试图为一个具有路径变量teacherId的方法构建一个超媒体链接,如下面的代码所示。我使用methodOn为getAllToturialByTeacher方法构建一个链接,例如,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);
}
}我发现了异常错误
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)无法设置占位符{teacherId}的值。有人能给我一个解决办法或者最好的方法来解决这个问题吗。
发布于 2015-11-09 08:39:45
在您的控制器中,teacherId不被注释为@PathVariable。
此外,您的链接创建看起来已损坏-请尝试如下:
resource.add(linkTo(methodOn(ToturialController.class).getAllToturialByTeacher(teacherId)).withRel("subjectsByTeacher"))https://stackoverflow.com/questions/33602885
复制相似问题