首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >spring-hateoas,如何在使用onMethod构建超媒体链接时设置位置持有者值

spring-hateoas,如何在使用onMethod构建超媒体链接时设置位置持有者值
EN

Stack Overflow用户
提问于 2015-11-09 05:10:50
回答 1查看 1.7K关注 0票数 0

我正在做一个restful应用程序,我一直试图为一个具有路径变量teacherId的方法构建一个超媒体链接,如下面的代码所示。我使用methodOngetAllToturialByTeacher方法构建一个链接,例如,http://localhost:8080/springexample/teachers/2/toturials

代码语言:javascript
复制
@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);
    }
}

我发现了异常错误

代码语言:javascript
复制
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}的值。有人能给我一个解决办法或者最好的方法来解决这个问题吗。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-11-09 08:39:45

在您的控制器中,teacherId不被注释为@PathVariable

此外,您的链接创建看起来已损坏-请尝试如下:

代码语言:javascript
复制
resource.add(linkTo(methodOn(ToturialController.class).getAllToturialByTeacher(teacherId)).withRel("subjectsByTeacher"))
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33602885

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档