0
votes

In my Spring HATEOAS project, I'm sending the following URI for a GET request,

http://localhost:8080/emp_db/api/v4/employees/?firstName=Tom

But, the link in the response shows up as follows,

_links": {
        "employees": {
            "href": "http://localhost:8080/emp_db/api/v4/employees/{?firstName,lastName,email}",
            "templated": true
        }
    }

It seems to be showing the query parameters inside as a Path Variable. Is this correct? I was expecting the link to be more like,

http://localhost:8080/emp_db/api/v4/employees/?firstName=Tom,lastName=,email=

My code is as follows,

@GetMapping 
    public CollectionModel<EntityModel<Employee>> get(@RequestParam Optional<String> firstName,
                                                      @RequestParam Optional<String> lastName,
                                                      @RequestParam Optional<String> email) {
        
        LOGGER.trace("Getting employees: {} {} {}", firstName, lastName, email);
        
        return CollectionModel.of(employeeService.findByFieldsLike(firstName, lastName, email)
                                                 .stream()
                                                 .map(assembler::toModel)
                                                 .collect(Collectors.toList()),
                                                 linkTo(methodOn(APIControllerV4.class)
                                                            .get(Optional.empty(), Optional.empty(), Optional.empty()))
                                                            .withRel("employees"));
    }
    
    
    @GetMapping("/{id}")
    public EntityModel<Employee> get(@PathVariable long id) {
        
        LOGGER.trace("Getting employee: {}", id);
        
        return employeeService.findById(id)
                              .map(assembler::toModel)
                              .orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND,
                                                                             String.format("Employee %d not found", id)));
    }

Override
    public EntityModel<Employee> toModel(Employee entity) {

        return EntityModel.of(entity, linkTo(methodOn(APIControllerV4.class).get(entity.getId())).withSelfRel(),
                                      linkTo(methodOn(APIControllerV4.class)
                                                .get(Optional.empty(), Optional.empty(), Optional.empty()))
                                                .withRel("employees"));
    }
You do not pass a value, why do you expect one to be present in the link? - a better oliver