Search code examples
springoverridingspring-restcontrollerspring-rest

How can I properly override a method declared in an abstract generic restcontroller?



I'm having some trouble implementing a function over some pre-existing code.

Other programmers working on this project previously defined a genric abstract "restcontroller" (it's not actually annotated as @RestController but it's meant to be extended by classes with that annotation)

public abstract class AbstractController<T extends AbstractEntity, R extends JpaRepository<T, Integer>> {
    @GetMapping(value = "/getall")
    public Paging<T> getAll(@RequestParam Integer itemsPerPage,
                            @RequestParam Integer pageIndex,
                            @RequestParam Map<String, String> filters,
                            @Autowired Consumer consumer) {

        //Fetch entities of type T from repository R and return them
    }

    //other generic crud operations

}

This class is usually extended by concrete controllers that simply define other operations on their specific types, but do no alter generic crud operations.
What I want to do is extend this class, but override the getAll method, like this:

@RestController
@RequestMapping("/api/tasks")
public class TaskController extends AbstractController<Task, TaskRepository> {
    @Override
    public Paging<Task> getAll(Integer itemsPerPage, Integer pageIndex, Map<String, String> filters, Consumer consumer) {
        LoggerFactory.getLogger(LazyTaskController.class).log("function called successfully!");
        Paging<Task> paging = super.getAll(itemsPerPage, pageIndex, filters, consumer);
        //do things with return value before returning
        return paging;
    }

}

If I call BASEURL/api/tasks/getall?itemsPerPage=25&pageIndex=0 without overriding the getAll method, the parameters are wired correctly (the Map contains two values, itemsPerPage and pageIndex, as expected, and consumer contains a concrete implementation of the intercace Consumer).
However if I do override it, the Map for some reason contains two values, one with key "consumer" and type Proxy, and another with key "org.springframework.validation.BindingResult.consumer" and value of type BeanPropertyBindingResult; and consumer contains a Proxy.

I suppose the @Override interferes with the autowiring of Consumer, but I can't figure out how to properly achieve what I have in mind (manipulating the results of getAll before returning them).
Thank you in advance


Solution

  • Nevermind, I solved it.
    The problem with the Map was solved by adding @RequestParam and @Autowired annotations to the overridden method parameters as well.
    The problem with the Consumer concrete type was somehow solved by applying a custom annotation that I found on another class in the codebase, I'm still not sure about what that annotation does but at least I know what to look for now.