How to Remove Repeated code in Spring Boot

I have a bunch of controllers where I include the following

public class AController{
@GetMapping
public List<A> getallA() {...}

@GetMapping("/{index}")
public A getA(Long index){...}
}

and then that is repeated

public class BController{
@GetMapping
public List<B> getallB() {...}

@GetMapping("/{index}")
public B getB(Long index){...}
}

Is there a way to remove the repeated code through a custom annotation? Perhaps, a generic implementation? I have never done this so the help would be very helpful. Ideally it would be something like

@GetFunctions(A.something)
public class AController{
}

Upvotes: 0

Views: 487

Answers (1)

Arun Prasat
Arun Prasat

Reputation: 360

Yes you can do something like pass name of the parameter as a path variable it will work try like the below example, Removing duplication from Spring controllers

If you want to have @RequestMappings with multiple values of attributes,

@RequestMapping({ "/index", "/contact" })

you can use like the above code snippet

Upvotes: 1

Related Questions