0

I am trying to filter the api by using four parameters: cloud provider, region, customer name and SAN.

When I send a GET request via Postman I can only seem to get a successfull HTTP response if I pass all four parameters into the url. How can I make all four params optional so that I can only look for one param if needed e.g. {{baseUrl}}/tenants/test2?region=CA_CENTRAL_1

I've tried adding (required=false) to the request params but that doesn't seem to work.

Any help is would be much appreciated. :)

Repository:

public interface TenantRepository extends JpaRepository<Tenant, Long> {
    List<Tenant> findByCloudAndRegionAndCustomerNameAndSfSan(CloudEnum cloud, Regions region, String customerName, String sfSan);
}

Controller:

    public ResponseEntity<?> findTenantsByCloudAndRegionAndCustomerNameAndSAN(
        @Parameter(in = ParameterIn.QUERY, description = "fetch cloud provider" ,schema=@Schema()) 
        @Valid @RequestParam( required=false ) String cloud, 

    @Parameter(in = ParameterIn.QUERY, description = "fetch region" ,schema=@Schema()) 
    @Valid @RequestParam( required=false) String region, 

     @Parameter(in = ParameterIn.QUERY, description = "fetch customer name" ,schema=@Schema()) 
    @Valid @RequestParam( required=false) String customerName,

     @Parameter(in = ParameterIn.QUERY, description = "fetch SAN" ,schema=@Schema()) 
     @Valid @RequestParam( required=false) String sfSan ) {     

    List<Tenant> tenantList  = tenantRepository.findByCloudAndRegionAndCustomerNameAndSfSan(CloudEnum.valueOf(cloud), Regions.valueOf(region), customerName, sfSan);
            
    return new ResponseEntity<List<Tenant>>(tenantList, HttpStatus.OK);     
}

Api:

@Operation(summary = "", description = "Returns a tenant based on 4 params", security = {
        @SecurityRequirement(name = "oAuth2ClientCredentials", scopes = {
            "delivery_tenants_api_read",
"delivery_tenants_api_write"        })    }, tags={  })
    @ApiResponses(value = { 
        @ApiResponse(responseCode = "200", description = "tenant response", content = @Content(schema = @Schema(implementation = Tenant.class))),
        @ApiResponse(responseCode = "200", description = "unexpected error", content = @Content(schema = @Schema(implementation = Error.class))) })
    @RequestMapping(value = "/tenants/test2",
        produces = { "application/json" }, 
        method = RequestMethod.GET)
    ResponseEntity<?> findTenantsByCloudAndRegionAndCustomerNameAndSAN(
        @Parameter(in = ParameterIn.QUERY, description = "fetch tenant by cloud provider", schema=@Schema()) 
        @Valid @RequestParam( required=false) String cloud, 
        @Parameter(in = ParameterIn.QUERY, description = "fetch tenant by region", schema=@Schema()) 
        @Valid @RequestParam( required=false) String region, 
        @Parameter(in = ParameterIn.QUERY, description = "fetch tenant by customer name", schema=@Schema()) 
        @Valid @RequestParam( required=false) String customerName, 
        @Parameter(in = ParameterIn.QUERY, description = "fetch tenant by SAN", schema=@Schema()) 
        @Valid @RequestParam( required=false) String sfSan );
Silvia
  • 1
  • Have you tried to see if there is a "required" attribute in the parameter annotation that you can set to false? I know there is one in @RequestParam, I don't know this annotation though – Matteo NNZ Jun 18 '22 at 21:21
  • It is because your underline query will be based on `And` condition and other 3 parameters would be null(or empty). for e.g. `CloudAndRegionAndCustomerNameAndSfSan` Your response would be returned only when all parameters would be non-empty. I'd suggest you can implement custom query by using `@Query` annotation on your JPA definition. Perform validations to check which parameters are non-empty and accordingly call your relevant JPA. Alternatively, Try this `findAllByCloudOrRegionOrCustomerNameOrSfSan` – Harsh Jun 19 '22 at 03:22
  • @MatteoNNZ Thanks for your comment Matteo. In the controller I tried setting the RequestParams to false by doing this: @RequestParam( required=false ). But it doesn't seem to work properly because I get a 500 internal server error. – Silvia Jun 19 '22 at 20:30

0 Answers0