[英]spring-hateoas, How to set place holder value when building hypermedia link using onMethod
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. 我正在做一个宁静的应用程序。我一直在尝试为具有pathvariable teacherId
的方法建立超媒体链接,如下面的代码所示。 I used methodOn
to build a link for getAllToturialByTeacher
method, for example, http://localhost:8080/springexample/teachers/2/toturials
我用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);
}
}
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}
. 我无法为占位符{teacherId}
设置值。 Could anyone give me a solution or best way to handle this. 任何人都可以给我解决方案或最佳方式来解决此问题。
In your controller the teacherId is not annotated as @PathVariable
. 在您的控制器中,TeacherId未被注释为@PathVariable
。
Also your link creation looks broken - try this: 另外,您的链接创建看起来很糟糕-尝试以下操作:
resource.add(linkTo(methodOn(ToturialController.class).getAllToturialByTeacher(teacherId)).withRel("subjectsByTeacher"))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.