Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Creating links with query params that are objects #1815

Open
johannesgiani opened this issue Jul 14, 2022 · 2 comments
Open

Creating links with query params that are objects #1815

johannesgiani opened this issue Jul 14, 2022 · 2 comments

Comments

@johannesgiani
Copy link

johannesgiani commented Jul 14, 2022

I want to create a link to a controller method that does not use single @RequestParam annotations for each query parameter. Instead I pass an object containing all query parameters. This works fine despite that the self link I am constructing does not contain the query parameters that I am passing:

  @GetMapping
  public PagedModel<EntityModel<FooDto>> findBy(
      @Valid Query query,
      @PageableDefault(size = 5) Pageable pageable,
      PagedResourcesAssembler<FooDto> assembler) {
    final Link link =
        linkTo(methodOn(Controller.class).findBy(query, pageable, assembler))
            .withSelfRel()
            .expand();
   
    final Page<FooDto> fooList = fooService.findBy(query, pageable);
    return assembler.toModel(fooList, link);
  }

The result I get when requesting http://localhost:8080/foo?param1=bar looks like this:

{
    "_links": {
        "self": {
            "href": "http://localhost:55560/foo"
        }
    },
    "page": {
        "size": 5,
        "totalElements": 0,
        "totalPages": 0,
        "number": 0
    }
}

It does not contain the query parameters in the self link. Any chance to make this work? I cannot find any documentation on query parameters when building links expect this one here: https://docs.spring.io/spring-hateoas/docs/current/reference/html/#server.link-builder.webmvc.methods.request-params which does not help me.

Thanks in advance :)

@dingjiefeng
Copy link

I want to create a link to a controller method that does not use single @RequestParam annotations for each query parameter. Instead I pass an object containing all query parameters. This works fine despite that the self link I am constructing does not contain the query parameters that I am passing:

  @GetMapping
  public PagedModel<EntityModel<FooDto>> findBy(
      @Valid Query query,
      @PageableDefault(size = 5) Pageable pageable,
      PagedResourcesAssembler<FooDto> assembler) {
    final Link link =
        linkTo(methodOn(Controller.class).findBy(query, pageable, assembler))
            .withSelfRel()
            .expand();
   
    final Page<FooDto> fooList = fooService.findBy(query, pageable);
    return assembler.toModel(fooList, link);
  }

The result I get when requesting http://localhost:8080/foo?param1=bar looks like this:

{
    "_links": {
        "self": {
            "href": "http://localhost:55560/foo"
        }
    },
    "page": {
        "size": 5,
        "totalElements": 0,
        "totalPages": 0,
        "number": 0
    }
}

It does not contain the query parameters in the self link. Any chance to make this work? I cannot find any documentation on query parameters when building links expect this one here: https://docs.spring.io/spring-hateoas/docs/current/reference/html/#server.link-builder.webmvc.methods.request-params which does not help me.

Thanks in advance :)

Did you solve it, I have the same problem

@mawsfr-adeo
Copy link

Hello !

Any update ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants