I have a scenario, in which I need to throw an exception when a method returns a null object and also customise the response based on the parameters sent to the method.
I have an interface which has some methods, now i want to ensure that whoever implements that method does a basic check (defined by annotation or something like that) like null check is assigned. So that after the overridden method is call that check is invoked
For example:
@NotNull
User getUser(Integer id)
if response is Null then
"User not available for id:" + id
I've tried using javax validation @NotNull but it doesn't provide the parameters sent to method, is there any way to achieve that using javax validation.
Another thing, I tried was using AOP @Around control annotation and was successful to get the response and the parameters, wanted to ensure is there any other way to achieve that, as pointed out annotation on interface not possible on AOP LINK.
Another option is using decorator pattern but involves lots of boiler plate coding
@InterceptorBinding
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE})
public @interface MethodInterceptorBinding { }
@Interceptor
@MethodInterceptorBinding
public class MethodInterceptor {
@AroundInvoke
public Object validateMethodInvocation(InvocationContext ctx)
throws Exception {
}
}
Tried CDI but the MethodInterceptor is not getting invoked in the spring boot application, do i need to register these interceptors, if yes then how?