Implementing Interceptor in Hybris
- 29th Jul 2018
- 5
- 69340
- how we can implementation Init Defaults Interceptor with example how we can implementation Load Interceptor with example how we can implementation Prepare Interceptor ? how we can implementation Prepare Interceptor with example how we can implementation remove Interceptor with example how we can implementation validate Interceptor with example Hybris interceptors implementation Implementing Interceptor in Hybris
As we learned in Interceptors-in-Hybris article, We have different types of interceptors in Hybris which will be called automatically during specific step of model life cycle.
Hybris has provided 5 interfaces to intercept at specific step of model lifecycle.
We need to create a new class and implement the suitable interface out of those 5 interfaces as per our requirement
If we want to intercept model while saving it, we can use PrepareInterceptor
If we want to perform some logic before model is being removed from DB, we can use RemoveInterceptor
Similarly, other interfaces also can be chosen based on where exactly we need to intercept the model lifecycle.
Let’s implement PrepareInterceptor
We know that, Prepare interceptor will be called while saving the model
Requirement :
Save consignment carrier code to Consignment model while saving it. Carrier code is available in CarrierModel
Step 1
Create a new Java class which implements PrepareInterceptor and override onPrepare() method.
- package de.hybris.platform.consignmenttrackingservices.interceptor;
- import de.hybris.platform.consignmenttrackingservices.model.CarrierModel;
- import de.hybris.platform.ordersplitting.model.ConsignmentModel;
- import de.hybris.platform.servicelayer.interceptor.InterceptorContext;
- import de.hybris.platform.servicelayer.interceptor.InterceptorException;
- import de.hybris.platform.servicelayer.interceptor.PrepareInterceptor;
- public class ConsignmentPrepareInterceptor implements PrepareInterceptor<ConsignmentModel>
- {
- @Override
- public void onPrepare(ConsignmentModel consignment, InterceptorContext ctx) throws InterceptorException
- {
- final CarrierModel carrier = consignment.getCarrierDetails();
- final String carrierCode = carrier == null ? null : carrier.getCode();
- consignment.setCarrier(carrierCode);
- }
- }
package de.hybris.platform.consignmenttrackingservices.interceptor; import de.hybris.platform.consignmenttrackingservices.model.CarrierModel; import de.hybris.platform.ordersplitting.model.ConsignmentModel; import de.hybris.platform.servicelayer.interceptor.InterceptorContext; import de.hybris.platform.servicelayer.interceptor.InterceptorException; import de.hybris.platform.servicelayer.interceptor.PrepareInterceptor; public class ConsignmentPrepareInterceptor implements PrepareInterceptor<ConsignmentModel> { @Override public void onPrepare(ConsignmentModel consignment, InterceptorContext ctx) throws InterceptorException { final CarrierModel carrier = consignment.getCarrierDetails(); final String carrierCode = carrier == null ? null : carrier.getCode(); consignment.setCarrier(carrierCode); } }
In this class, we have taken carrier code from CarrierModel and setting it to ConsignmentModel explicitly.
Step 2
Register and declare the ConsignmentPrepareInterceptor as spring bean
- <!-- Defining Spring bean for Interceptor -->
- <bean id="consignmentPrepareInterceptor" class="de.hybris.platform.consignmenttrackingservices.interceptor.ConsignmentPrepareInterceptor"/>
<!-- Defining Spring bean for Interceptor --> <bean id="consignmentPrepareInterceptor" class="de.hybris.platform.consignmenttrackingservices.interceptor.ConsignmentPrepareInterceptor"/>
Step 3
Map this interceptor with corresponding model
- <!-- Mapping Interceptor and model class-->
- <bean id="consignmentPrepareInterceptorMapping" class="de.hybris.platform.servicelayer.interceptor.impl.InterceptorMapping">
- <property name="interceptor" ref="consignmentPrepareInterceptor"/>
- <property name="typeCode" value="Consignment"/>
- </bean>
<!-- Mapping Interceptor and model class--> <bean id="consignmentPrepareInterceptorMapping" class="de.hybris.platform.servicelayer.interceptor.impl.InterceptorMapping"> <property name="interceptor" ref="consignmentPrepareInterceptor"/> <property name="typeCode" value="Consignment"/> </bean>
This mapping makes sure that, mapped interceptor will be intercepting only the mapped model’s lifecycle.
In this case, consignmentPrepareInterceptor will be intercepting the lifecycle of ConsignmentModel only
Step 4
We just need to build and start the server
Note :
If we implement ValidateInterceptor also for the same Model then ValidateInterceptor is always invoked after PrepareInterceptor. All the above steps are same to implement any of the above 5 interceptors provided by Hybris. We just need to override corresponding method and provide the logic as per the requirement
I couldn’t find where you need to create the Interceptor file, can you add the path where this needs to be created?
“Create a new Java class which implements PrepareInterceptor and override onPrepare() method.”
thank you KB. very nice article.
In step of mapping interceptors, what would be the exact issue we get if we miss this mapping? Any how we are operating on particular model in out implementation class.
Hi Kb ,
yours explanation on interceptor is very understandable and i have a small doubt in that.
Q) You have mentioned Validate interceptor always invoked after the Prepare Interceptor but in your earlier explanation(Hybris In Interceptor Link ) you said like validate interceptor is called before Prepare Interceptor(Prepare interceptor definition section) .
can you please clarify my doubt , Thanks in advance.
Thank you very much !