Spring Hateoas忽略查询参数

时间:2018-09-27 04:31:38

标签: java spring spring-boot hateoas spring-hateoas

Spring Hateoas没有为以下请求映射创建适当的链接:

@GetMapping("/member/{id}")
public MemberResource getMemberById(@RequestParam String id){
     Member member = MemberService.getMemberById(id);
     return new MemberResource(member);
}


public class MemberResource extends ResourceSupport{

     public MemberResource(Member member){
  add(linkTo(methodOn(MemberController.class).getMember(member.getId)).withSelfRel());
        add(linkTo(methodOn(TeamsController.class).getTeams(memberId)).withRel("teams"));
        }}


        @GetMapping("/teams")
        public List<TeamResource> getTeamsById(@RequestParam String id)          {
            List<Team> teams = teamService.getTeamsById(id);
            return teams.stream().map(TeamResource::new).collect(Collectors.toList());
        }

致电/ member / 12345时希望返回:

{
     member: {
       //omitted for brevity
     },
    _links:[
       {
         href: .../members/12345
       },
       {
         href: ../teams?memberId = 12345
       }

      ]
}

但是它返回:

{
     member: {
       //omitted for brevity
     },
    _links:[
       {
         href: .../members/12345
       },
       {
         href: ../teams/12345
       }

      ]
}

这是为什么?即使我使用@RequestParam,它也完全忽略了过滤器。在这件事上任何建议都值得赞赏。

1 个答案:

答案 0 :(得分:1)

免责声明:从未使用过Spring HATEOAS。

但是乍看之下,您似乎混淆了两种URL参数类型:

  • @PathVariable用于路径变量,例如.../teams/TEAM_ID
  • @RequestParam用于查询参数,例如.../teams?id=TEAM_ID