spring-projects / spring-projects/spring-hateoas

Creating links with query params that are objects

Open
#1,815 5 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Java
Stars
1.1k
Forks
476
PR merge metrics
No merged PRs in 30d

Description

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 :)

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linkTo(methodOn(Controller.class).findBy(query, pageable, assembler)).expand() call and reproduce the request at /foo?param1=bar. Trace how the Query object is handled when building the self link. Done means the generated href includes the supplied query parameters, with coverage for this object-based controller method.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
api, backend
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.