Extending the REST API
You can extend the REST API for Commerce Services using an AddOn.
Defining a Controller
Context
To expose new calls, define a Controller class with the appropriate methods.
Procedure
ExtendedCustomersController.java
/**
* Controller which extends Customer Resources
*/
@Controller("sampleExtendedCustomerController")
@RequestMapping(value = "/{baseSiteId}/customers")
public class ExtendedCustomersController
{
...
@Secured("ROLE_CUSTOMERGROUP")
@RequestMapping(value = "/current/nickname", method = RequestMethod.GET)
@ResponseBody
public String getCustomerNickname()
{
final String name = getCustomerFacade().getCurrentCustomer().getNickname();
return name;
}
@Secured("ROLE_CUSTOMERGROUP")
@RequestMapping(value = "/current/nickname", method = RequestMethod.PUT)
@ResponseBody
public CustomerData setCustomerNickname(@RequestParam final String nickname) throws DuplicateUidException
{
final CustomerData customer = customerFacade.getCurrentCustomer();
customer.setNickname(nickname);
customerFacade.updateFullProfile(customer);
return customerFacade.getCurrentCustomer();
}
...
}
Creating the Web Spring Context
Context
Since the sample controller is annotated as @Controller, Spring must be told where it should look for it.
Procedure
The file extends Commerce Services context. This file is not the standard web context file from the web\webroot\WEB-INF directory.
resource/occaddon/web/spring/occaddon-web-spring.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">
<context:component-scan base-package="de.hybris.platform.occaddon.controllers"/>
</beans>
Adding the Web Context to Commerce Services
Context
Extend Commerce Services context with the context from the newly created AddOn. You can accomplish this by using the additionalWebSpringConfigs mechanism. The project.properties file must contain the following line where Spring Config Classpath points to any classpath resource from the <addonExtension>:
<targetExtension>.additionalWebSpringConfigs.<addonExtension>=[Spring Config Classpath]
Procedure
project.properties.template
ycommercewebservices.additionalWebSpringConfigs.<addOnName>=classpath:/occaddon/web/spring/<addOnName>-web-spring.xml
This file is a template for the project.properties file, which is generated during the installation process.
Verify the New Endpoints Defined in the Controller
| Method | URL | Header | Body Parameter | Authentication | Description |
|---|---|---|---|---|---|
| POST | https://localhost9002/authorizationserver/oauth/token | Content-Type: application/x-www-form-urlencoded | client_id=$CLIENT_ID$&client_secret=$CLIENT_SECRET$&grant_type=password&username=example@sap.com&password=your_password | Retrieves an access token that will be sent for next requests. | |
| PUT | https:/localhost9002/occ/v2/electronics/users/8716fdb8-6cea-4e28-8396-1258e819f758/nickname
|
Content-Type: application/x-www-form-urlencoded | nickname=myNickName | OAUTH token for client | Update the nick name. |
| GET | https://localhost9002/occ/v2/electronics/users/8716fdb8-6cea-4e28-8396-1258e819f758/nickname
|
OAUTH token for client | Get the nickname for the given customer. |
Overriding the REST API
Context
In an AddOn for Commerce Services, you can override the existing calls. The only difference between extending and overriding the REST API is using the @RequestMappingOverride annotation.
Procedure
RequestMappingOverride
@Target(
{ ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
public @interface RequestMappingOverride
{
/**
* Name for property, which stores priority value
*/
String priorityProperty() default "";
}
The method with the highest priority is used.
The Priority value is read from the properties file (project.properties, local.properties files) based on:
- priorityProperty given in the annotation or
- property name requestMappingOverride.<className>.<methodName>.priority
Example:
requestMappingOverride.de.hybris.platform.occaddon.controllers.ExtendedCustomersController.updateDefaultAddress.priority
If there is no property defined in the properties file, the priority value is set to zero.
Example:
- @RequestMappingOverride(priorityProperty="occaddon.updateDefaultAddress.priority") - here the priority value is read from the occaddon.updateDefaultAddress.priority property.
- @RequestMappingOverride - here the priority value is read from requestMappingOverride.<className>.<methodName>.priority property (e.g. requestMappingOverride.de.hybris.platform.occaddon.controllers.ExtendedCustomersController.updateDefaultAddress.priority).
Overriding the Request Mapping Example
ExtendedCustomerController.java
/**
* Controller which extends Customer Resources
*/
@Controller("sampleExtendedCustomerController")
@RequestMapping(value = "/{baseSiteId}/customers")
public class ExtendedCustomersController
{
...
/**
* This is example of overriding existing request mapping. Annotation {@link RequestMappingOverride} allows override
* existing request mapping, defined by {@link RequestMapping} annotation.
*/
@Secured("ROLE_CUSTOMERGROUP")
@RequestMapping(value = "/current/addresses/default/{id}", method = RequestMethod.PUT)
@RequestMappingOverride
@ResponseBody
public String updateDefaultAddress(@PathVariable final String id) throws DuplicateUidException
{
final AddressData address = userFacade.getAddressForCode(id);
userFacade.setDefaultAddress(address);
return "Address was updated successfully by method from sampleExtendedCustomerController";
}
...
}
Assigning New Calls to a Specific API Version
Context
There are two versions available for Commerce Services (v1 and v2). Both versions are available simultaneously. As a result, you might need to specify the version to call from the AddOn. To do this, you can use the ApiVersion annotation.
/**
* Annotation can be used for controllers. It allows restrict visibility of methods annotated with
* {@code @RequestMapping} only to selected version of commerce web services (e.g. v1 or v2).
*/
@Target(
{ ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
public @interface ApiVersion
{
/**
* Returns API version for which methods from controller should be registered (e.g. v1).
*/
String value();
}
This annotation can be used at the controller level.
@Controller("sampleExtendedCustomerController")
@RequestMapping(value = "/{baseSiteId}/customers")
@ApiVersion("v1")
public class ExtendedCustomersController
{
...
}