REST – CUSTOM REST CREATION
1. Make entry of your custom handler in resources-ext.properties at below path.
Rest\WebContent\WEB-INF\config\resources-ext.properties
2. 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);
3. 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.
4. 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 {
.....
......
........
..........
}
5. 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
6. 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.
7. 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);
8. 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) ;
9. Restart the server.
10. 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"
}
Comments
Post a Comment