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,它也完全忽略了过滤器。任何建议在此问题上表示赞赏。

最佳答案

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

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


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

10-06 11:06