AOP and REST
As a module developer, you may face a situation where you want to extend the behavior of a lower-level module's REST resource, without changing its URL. You can use Aspect-Oriented Programming (AOP) to achieve this.
To begin, create a new class annotated with both org.springframework.stereotype.Component and org.aspectj.lang.annotation.Aspect. Add Pointcuts and other annotations as dictated by AspectJ. For example, the code below will run after a return from the "getSample()" method of SampleAopResource in module "platformtestmodule":
@Component
@Aspect
public
class
SampleAopResourceListener {
@Pointcut
(
"execution(com.onenetwork.platformtestmodule.rest.SampleAopResource.Sample com.onenetwork.platformtestmodule.rest.SampleAopResource.getSample())"
)
public
void
getSample() {
}
@AfterReturning
(pointcut =
"getSample()"
, returning =
"sample"
)
public
Sample afterReturning(Sample sample) {
sample.setValue(
"alteredSample"
);
return
sample;
}
}
Once this is in place, make sure this class package is included in your *-app-context.xml spring file:
...
<context:component-scan base-
package
=
"com.onenetwork.aopsamplemodule.mpt,com.onenetwork.aopsamplemodule.rest"
/>
...
Now, once you build and restart the server, your SampleAopResourceListener should be called at the conclusion of SampleAopResource.getSample(), before the values are provided to Jersey for serialization in the HTTP response.