This Document is to help any ATG developer in writing custom Rest Services.
1) Create a custom Service
For Example :
package common.utils;
import atg.nucleus.GenericService;
public class OrderService extends GenericService {
public void addOrder(String bookRef) throws Exception {
System.out.println("Hello , its my first webservice..");
}
}
3) Configure the security for the above component in restSecurityConfiguration.xml
<rest-security>
<resource component="/common/utils/OrderService" secure="false">
<default-acl value="Profile$role$admin:read,write,execute;Profile$role$admin:read,write,execute"/>
</resource>
</rest-security>
for the user and role please go through ATG docs , but the above will work for all diff env.
4) deploy the application in jboss server { make sure that the module you are deploying on the appserver includes REST module of the ATG}
5) once the server is up validate your service availability by checking http://localhost/rest/common/utils/OrderService
6) write the client using atg-rest-client
Example
package test;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import atg.rest.client.RestClientException;
import atg.rest.client.RestComponentHelper;
import atg.rest.client.RestResult;
import atg.rest.client.RestSession;
/**
* Method Call implementation through REST API
*
* @author ashish
*
*/
public class RESTClient {
/** The m username. */
private String mUsername;
/** The m password. */
private String mPassword;
/** The m host. */
private String mHost;
/** The m port. */
private int mPort;
/** The m session. */
private RestSession mSession = null;
/**
* Instantiates a new method call by rest.
*/
public RESTClient() {
}
/**
* Execute.
*
* @throws RestClientException the rest client exception
*/
private void execute() {
mSession = RestSession
.createSession(mHost, mPort, mUsername, mPassword);
mSession.setUseHttpsForLogin(false);
Map<String,Object> params = new HashMap<String,Object>();
params.put("atg-rest-input", "json");
RestResult result = null;
try {
result = RestComponentHelper.executeMethod("/thomascook/starfish/common/utils/OrderService", "addOrder", new
Object[] {}, params, mSession);
} catch (RestClientException e1) {
System.out.println(e1);
}
try {
if (result != null && result.getResponseCode() == 200) {
System.out.println("Executed Successfully.");
} else {
System.out
.println("Error while execution : Error Code ["
+ result.getResponseCode()
+ "] and Message ["
+ result.getResponseMessage() + "]");
}
} catch (IOException e) {
System.out.println("Error while execution Successfully.");
}
}
/**
* @param args
*/
public static void main(String[] args) {
RESTClient stepUtils = new RESTClient();
stepUtils.mUsername = "admin";
stepUtils.mPassword = "admin";
stepUtils.mHost = "localhost";
stepUtils.mPort = 8080;
stepUtils.execute();
}
}
Step 7) execute the above client and it will work.
if you follow the steps properly it will definitely work.
Any question please let me know.
1) Create a custom Service
For Example :
package common.utils;
import atg.nucleus.GenericService;
public class OrderService extends GenericService {
public void addOrder(String bookRef) throws Exception {
System.out.println("Hello , its my first webservice..");
}
}
2) Map it to the property file
$class=common.utils.OrderService
$scope=global
loggingInfo=true
3) Configure the security for the above component in restSecurityConfiguration.xml
<rest-security>
<resource component="/common/utils/OrderService" secure="false">
<default-acl value="Profile$role$admin:read,write,execute;Profile$role$admin:read,write,execute"/>
</resource>
</rest-security>
for the user and role please go through ATG docs , but the above will work for all diff env.
4) deploy the application in jboss server { make sure that the module you are deploying on the appserver includes REST module of the ATG}
5) once the server is up validate your service availability by checking http://localhost/rest/common/utils/OrderService
6) write the client using atg-rest-client
Example
package test;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import atg.rest.client.RestClientException;
import atg.rest.client.RestComponentHelper;
import atg.rest.client.RestResult;
import atg.rest.client.RestSession;
/**
* Method Call implementation through REST API
*
* @author ashish
*
*/
public class RESTClient {
/** The m username. */
private String mUsername;
/** The m password. */
private String mPassword;
/** The m host. */
private String mHost;
/** The m port. */
private int mPort;
/** The m session. */
private RestSession mSession = null;
/**
* Instantiates a new method call by rest.
*/
public RESTClient() {
}
/**
* Execute.
*
* @throws RestClientException the rest client exception
*/
private void execute() {
mSession = RestSession
.createSession(mHost, mPort, mUsername, mPassword);
mSession.setUseHttpsForLogin(false);
Map<String,Object> params = new HashMap<String,Object>();
params.put("atg-rest-input", "json");
RestResult result = null;
try {
result = RestComponentHelper.executeMethod("/thomascook/starfish/common/utils/OrderService", "addOrder", new
Object[] {}, params, mSession);
} catch (RestClientException e1) {
System.out.println(e1);
}
try {
if (result != null && result.getResponseCode() == 200) {
System.out.println("Executed Successfully.");
} else {
System.out
.println("Error while execution : Error Code ["
+ result.getResponseCode()
+ "] and Message ["
+ result.getResponseMessage() + "]");
}
} catch (IOException e) {
System.out.println("Error while execution Successfully.");
}
}
/**
* @param args
*/
public static void main(String[] args) {
RESTClient stepUtils = new RESTClient();
stepUtils.mUsername = "admin";
stepUtils.mPassword = "admin";
stepUtils.mHost = "localhost";
stepUtils.mPort = 8080;
stepUtils.execute();
}
}
Step 7) execute the above client and it will work.
if you follow the steps properly it will definitely work.
Any question please let me know.
HI ashish,
ReplyDeleteNeed some clarification on step 3.
1. making secure false will expose service to all user. So i want it to be TRUE.
2. Can u clarify which user we will be putting in ACL. End users i.e. customers or Some administrative users.
Deleteyou can set the value of secure=true and try again.
Can you pass any Nested JSON structure to the rest API exposed by ATG?
ReplyDeleteCan ATG consume the same?
Hi Ashish,
ReplyDeleteI am a beginner to ATG commerce, In order to customizing the rest service,what is the required module should have in my ATG environment?
include REST module and for more reference
ReplyDeletehttps://docs.oracle.com/cd/E26180_01/Platform.94/ATGIntFrameGuide/html/s1101partiiiatgplatformrestwebservice01.html