Writing REST handler: If you are writing a custom handle in WCS follow below steps:
- Make entry of your custom handler in resources-ext.properties at below path.
Rest\WebContent\WEB-INF\config\resources-ext.properties
e.g com.mycomp.commerce.rest.handlers.ExMyHandler
If you want this to be served only over HTTPS/ support partial authentication, then entry has to be made in wc-rest-security.xml (/Rest/WebContent/WEB-INF/config/com.ibm.commerce.rest-ext/wc-rest-security.xml) with the API PATH. - Create a Rest Handler by extending AbstractConfigBasedClassicHandler only when rest handler invokes a data bean or controller command and use below method to call the resource.
executeConfigBasedBeanWithContext(
RequisitionListDataBean.class.getName(),
profileName,
responseFormat,
null); - Create a Rest Handler by extending AbstractResourceHandler when the logic exist in rest handler, I mean when Rest Handler not using any Data Bean or Commands.
- Create a Java class which extends AbstractConfigBasedClassicHandler or AbstractResourceHandler as per you need. Also set the @Path and @Description annotation.
@Path("api/test")
@Description("This is a simple RESTful service")
public class ExMyHandler extends AbstractConfigBasedClassicHandler {
.....
......
........
..........
} - Override the getResourceName() method which returns the Resource Name :-
@Override
public String getResourceName() {
return RESOURCE_NAME;
}
Note: Make sure the getResourceName() is not returning null. Other wise Swagger UI will fails. The resource name should be part of the rest handler path. - Create a method to be called with the correct @Path , @Produces, @GET/@POST annotations. The return type should be Response type, refer below code snippet :-
@GET
@Produces(MediaType.APPLICATION_JSON)
@Description("Retrieve welcome message")
@Path("welcomeMessage")
public Response getTestMessage(@QueryParam("name") String name) throws Exception {
..
..
..
}
You can also include @PathParam as well in the method definition. I just used QueryParam in the above example as the name will be passed as a query parameter. - Create a map with the response values. Please refer below code snippet :-
Map testMap= new HashMap();
testMap.put("name",name);
testMap.put("welcomeMessage","Welcome " + name); - Generate the response object by calling the generateResponseFromHttpStatusCodeAndRespData() with the return type, response values(map) and http status (200 , if OK status). Please refer below code snippet :-
return generateResponseFromHttpStatusCodeAndRespData(MediaType.APPLICATION_JSON, map, httpStatus) ;
- Restart the server.
- Call the API using POSTMAN or Swagger UI or just browser (if it is GET)
URL: http://localhost/wcs/resource/api/test/welcomeMessage?name=Lokesh
Response will be like:-
Response :
{
"name": "Lokesh",
"welcomeMessage": "Welcome Lokesh"
}
Please let me know in comments if you face any issue by doing above steps.