0

I'm currently learning spring boot and encounter an error message

method count(JobViewWrapper) is already defined

yes it's because it has 2 of the same method name , but the method has 2 separate function , the first one is to count all the job with deleted flag 1. the second one (count-active), is to count all the job with deleted flag 1 and is active flag 1.

So i needed this 2 method, is there a workaround to do it?

@PostMapping(value = "/count")
public long count(@RequestBody(required = false) JobViewWrapper wrapper) {
    System.out.println("into controller count");
    if (wrapper == null) {
        wrapper = new JobViewWrapper();
    }
    System.out.println("Prepare to count service");
    return JobService.countLazyView();
}
    
@PostMapping(value = "/count-active")
public long count(@RequestBody(required = false) JobViewWrapper wrapper) {
    System.out.println("into controller count");
    if (wrapper == null) {
        wrapper = new JobViewWrapper();
    }
    System.out.println("Prepare to count service");
    return JobService.countLazyViewIsActive();
}

my service

public long countLazyView() {
    return lowonganKerjaRepo.countLazyView();
}
    
public long countLazyViewIsActive() {
    return lowonganKerjaRepo.countLazyViewIsActive();
}
Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
Maxes
  • 23
  • 6
  • 2
    Call the second one `countActive` instead? – Federico klez Culloca Oct 30 '20 at 07:47
  • 2
    you cannot have 2 methods with the same signature (name + arguments) in the same class. Rename one of them – pedrohreis Oct 30 '20 at 07:59
  • Federico, i used 2 because i needed the /count for counting all of them in repo it's only has filtering "where deleted=1" and the method with value /count-active has query in repo "where deleted=1 and is_active=1" So it would make the count difference record number – Maxes Oct 30 '20 at 08:06
  • pedrohreis, JobViewWrapper is actually a class name, and i tried to rename the "wrapper" in that method, like "wrapper1" but still the same error Any way to work around this? – Maxes Oct 30 '20 at 08:07

1 Answers1

1

If you really want to overload method count you must select one of the three options available:

  1. provide different number of parameters:
@PostMapping(value = "/count")
public long count(@RequestBody(required = false) JobViewWrapper wrapper) {
// ...
    return JobService.countLazyView();
}

@PostMapping(value = "/count-active")
public long count() {
    return JobService.countLazyViewIsActive();
}
  1. provide different types of parameters:
@PostMapping(value = "/count")
public long count(@RequestBody(required = false) JobViewWrapper wrapper) {
// ...
    return JobService.countLazyView();
}

@PostMapping(value = "/count-active")
public long count(@RequestBody(required = false) ActiveJobViewWrapper wrapper) {
    return JobService.countLazyViewIsActive();
}
  1. provide different order of parameters (seems to be not applicable in this case).

If none of these options can be selectable, you'll have options:

  1. Provide different names for these methods count() and countActive

  2. Replace these methods with one method having additional parameter (API call may be changed to /count?active=true):

@PostMapping(value = "/count")
public long count(
    @RequestParam(name = "active", required = false, defaultValue = "false") Boolean active, 
    @RequestBody(required = false) JobViewWrapper wrapper) {
// ...
    
    return active ? JobService.countLazyViewIsActive() : JobService.countLazyView();
}
Nowhere Man
  • 19,170
  • 9
  • 17
  • 42
  • So..yeah i used that number 2 solution because that will be the most easy and simplest way. I make a new class for that. Thankyou Alex, you're a life saver.. – Maxes Nov 03 '20 at 01:58