Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

147
Visualizações
How to differentiate edpoints with the same name but different query params in spring boot

I'm new to spring boot and trying to make a Rest API with some resources, I have the following:

Get all existing rules:

 @GetMapping("/rules")
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "Get all the existing rules", response = RuleViewModel.class) //Swagger documentation
    public  ResponseEntity<Page<RuleViewModel>> get(@PageableDefault(page = 0, size = 25) Pageable pageable) {
        Page<Rule> rulesEntity = ruleService.findByDeletedIsSend(false, pageable);
        Page<RuleViewModel> ruleViewModels =  rulesEntity.map(mapper::ruleViewModelMapper);
        return new ResponseEntity<>(ruleViewModels, HttpStatus.OK);
    }

Filter all the existing rules with different parameters

@GetMapping(value = "/rules")
@ResponseStatus(HttpStatus.OK)
@ApiOperation(value = "Get a specific rule by filter", response = RuleViewModel.class)
public ResponseEntity<Page<RuleViewModel>> getByFilter(
        @RequestParam(name = "id", required = false) Optional<String> ruleId,
        @RequestParam(name = "description", required = false) Optional<String> description,
        @RequestParam(name = "types", required = false) Optional<List<Boolean>> types,
        @RequestParam(name = "layers", required = false) Optional<List<Layer>> layers,
        @RequestParam(name = "groups", required = false) Optional<List<Category>> groups,
        Pageable pageable
) {
    Page<Rule> rulesEntities = ruleService
            .filterRule(
                    types.orElse(Arrays.asList(true, false)),
                    layers.orElse(layerService.findByIsActive(true)),
                    groups.orElse(categoryService.findByIsActive(true)),
                    description.orElse(""),
                    ruleId.orElse(""),
                    false,
                    pageable
            );
    Page<RuleViewModel> ruleViewModels = rulesEntities.map(mapper::ruleViewModelMapper);
    return ruleViewModels.getSize() > 0 ? new ResponseEntity<>(ruleViewModels, HttpStatus.OK) : new ResponseEntity<>(HttpStatus.NOT_FOUND);
}

As you can see the path is the same ("/rules") but in the case of the filters, it adds all the optional parameters.

The problem I have is that when I want to use filter params as:

http://localhost:5656/v1/rules?id=7be4336d-6495-5b71-9bc2-a97c9da5ede2

It always goes to get all rules endpoint.

The workaround I made was to add the "search" path to URI like this:

http://localhost:5656/v1/rules/search?id=7be4336d-6495-5b71-9bc2-a97c9da5ede2

So the requests get correctly mapped, but I think it violates the Rest best practices.

Could you point me in the right way to achieve this?. Thanks in advance.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Since both the methods have the same return type and all parameters to the getByFilter method are optional, why don't you remove the getByFilter method and add the parameters as optional parameters to the get method? The get method can then perform the filter logic if any of the optional parameters are passed in, else return the complete response without any filtering.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda