I'm trying to access a path variable from a request URL defined at the controller class level, from a method annotated with @PostConstruct. The idea is to use this path variable to initialize a local variable at the start of the API call before the relevant controller method is executed. This is to avoid having that piece code in every method and just let the controller set the variable at the start of the call.
The code might explain this better:
@RestController
@RequestMapping("/path/{variable}")
public class BaseController {
protected Object object;
@PostConstruct
protected void setObject(@PathVariable("variable") Long variable){
this.object= objectRepository.findById(variable).get();
}
}
Obviously at this point I'm running into the "Lifecycle method annotation requires a no-arg method" error - so I know @PostConstruct is not the way to go, but I'm wondering if there's another way to achieve this, perhaps something like @Before for unit tests?
Thanks in advance!