Home > Enterprise >  @RequestMapping with "param" parameter not working correctly
@RequestMapping with "param" parameter not working correctly

Time:05-09

I have the following spring-boot REST Controller:

@RestController
@AllArgsConstructor
public class AuditRecordArchivalHistoryController {
  private final AuditRecordArchivalHistoryService auditRecordArchivalHistoryService;

  @RequestMapping(value = "/archives", params = "pageNumber")
  public Mono<Slice<AuditRecordArchivalHistory>> findRecordsForAllTenants(
        @RequestParam (defaultValue = "0") Integer pageNumber)
    return 
      auditRecordArchivalHistoryService.findAllAuditRecordArchivalHistoryRecords(
            getPageable(pageNumber)
    );
}

@RequestMapping(value = "/archives", params = {"tenantId, pageNumber"} )
public Mono<Slice<AuditRecordArchivalHistory>> findRecordsForTenant(
        @RequestParam String tenantId,
        @RequestParam Integer pageNumber) {

    return 
      auditRecordArchivalHistoryService.findAuditRecordArchivalHistoryByTenantId(
            tenantId, getPageable(pageNumber)
    );
}

I expect that if I access the URL

/archives?pageNumber=1

the method: findRecordForAllTenants() would be called.

Whereas my expectation upon accessing the URL

/archives?tenantId=abc&pageNumber=1

is that the method: findRecordsForTenant() would be called.

However, in both the cases, the method findRecordsForAllTenants() is being called. Am I missing something?

CodePudding user response:

I think this problem is because the parameters in params are written wrong, you can compare to what I wrote below.

@RequestMapping(value = "/archives", params = {"tenantId", "pageNumber"} )
public Mono<Slice<AuditRecordArchivalHistory>> findRecordsForTenant(
    @RequestParam String tenantId,
    @RequestParam Integer pageNumber) {
return auditRecordArchivalHistoryService.findAuditRecordArchivalHistoryByTenantId(
        tenantId, getPageable(pageNumber));
}

CodePudding user response:

You have put "tenantId, pageNumber", it should be "tenantId", "pageNumber". You are missing quotes.

CodePudding user response:

Mast be like:

@RequestMapping(value = "/archives")
  public Mono<Slice<AuditRecordArchivalHistory>> findRecordsForAllTenants(
        @RequestParam (name="pageNumber", defaultValue = "0") Integer pageNumber)
    return 
      auditRecordArchivalHistoryService.findAllAuditRecordArchivalHistoryRecords(
            getPageable(pageNumber)
    );
  • Related