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