Friday 22 September 2017

JAX-RS Basics - Getting Started : Sample Program

Hi All,
     
        Hope all are doing well, Today we are going to see some Interesting Basics of the JAX-RS .

JAX-RS Implementation was Introduced in the Oracle Commerce Platform 11.3 , with this Standard Oracle was able to competate the Industrial Standards,   in the REST Services . So Lets Deep in to .

1) The First Step to start with the JAX-RS Application is add the Following entry in the web.xml

<servlet>
    <servlet-name>Jersey</servlet-name>
    <servlet-class>atg.service.jaxrs.JerseyServletWrapper</servlet-class>
    <init-param>
        <param-name>atg.service.jaxrs.JerseyServletWrapper.servletClasses
</param-name>
        <param-value>org.glassfish.jersey.servlet.ServletContainer,
atg.service.swagger.SwaggerBootstrap</param-value>
    </init-param>
    <!-- Params for JerseyServletWrapper servlet -->
    <init-param>
        <param-name>javax.ws.rs.Application</param-name>
        <param-value>atg.service.jaxrs.JAXRSApplication</param-value>
    </init-param>
    <init-param>
        <param-name>jersey.config.server.provider.classnames</param-name>
        <param-value>atg.service.swagger.SwaggerApiListingResource,
io.swagger.jaxrs.listing.SwaggerSerializers</param-value>
    </init-param>
    <init-param>
        <!-- Params for JAXRSApplication -->
        <param-name>atg.service.jaxrs.JAXRSApplication.resourceRegistryPath
</param-name>
        <param-value>/atg/dynamo/service/jaxrs/RestResourceRegistry</param-value>
    </init-param>
    <init-param>
        <param-name>atg.service.jaxrs.JAXRSApplication.validatorRegistryPath
</param-name>
        <param-value>/atg/dynamo/service/validator/ValidatorRegistry</param-value>
    </init-param>
    <init-param>
        <!-- Params for SwaggerBootstrap servlet -->
        <param-name>atg.service.swagger.SwaggerBootstrap.bootstrapService
</param-name>
        <param-value>/atg/dynamo/service/swagger/SwaggerBootstrapService
</param-value>
    </init-param>
    <load-on-startup>2</load-on-startup>
    <async-supported>true</async-supported>
</servlet>
<servlet-mapping>
    <servlet-name>Jersey</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

Once if you add the Following entry then the JAX-RS is enabled in the application, we can go ahead and creating our Resource classes . 

From the above XML there is a path for defining the RestResourceRegistry,Validatory Registry and SwaggerBootStratService , if you are defining your Custom you can update to point yours. For SwaggerBootStratService and ValidatoryRegistry give the OOTB Component path this is required till you start diving in to more . 

2)You can add the mentioned classes  are avaliable in the DAS, Rest Modules you can add the Reference in the Build Path once it is done ,these will be available for your project  .Make Sure you Resolve the Dependency issue  of the Jars. 

3)Start Creating the Component in the path /atg/dynamo/service/jaxrs/RestResourceRegistry
this is the Component where we have register the Components created on behalf of the JAX-RS implementaiton.

nucleusRestResources : this property hold those components .

Those components can be created by the Following way 

4)Create a class extends GenericService

@RestResource(id=OrderRestConstants.CURRENT_ORDER_ID)
@Path("/cart")
@Api(value="/cart")
public class CurrentOrderRestResource extends GenericService {
}

Here@RestResource – must be present for all resource classes, as it specifies the ID of the
resource. The ID is a string that uniquely identifies the resource and does not change when the context and/or version change.Try to give the package name as well.

@Path . The annotation contains the URI path for the resource. For example, @path ("/cart"). Note: If you are creating a sub-resource,ensure that there is no @Path annotation.

Create like above example . Next Will Start Writing the methods 

@GET
@Endpoint(id="/cart#GET" filterId="cart-Default")
@ApiOperation(value="Gets the current order.")
public Object getOrder(JSONObject pJson) throws RestException, CommerceException {
}

@GET, @POST, @PUT, @PATCH, and, @DELETE – These annotations are used as necessary when working with the endpoint.

@Endpoint – This annotation identifies the endpoint with a unique string ID. The ID does not change if the context or version changes. If the endpoint is for a singular resource, the isSingular attribute must be true.

5)A resource’s schema is controlled using the ValidatorManager component and payloadSchema.xml files.These XML files configure multiple validators that contain the definitions for expected input fields, as well as required output fields. Endpoints can specify which validator definition to use for input, using the validatorId attribute, and for output, using the filterId attribute.Will Discuss Brief more about this in the upcoming Sessions .

6)Write the Logic here and return type should be the Object , this is required because we are returning the RepresentationModel . 


The RepresentationModel is a holder class that allows you to build a representation of a resource for aresponse. It contains properties that specify the resource state, embedded resources, collection members, links,headers, and other properties. If an endpoint needs to respond with a representation of the resource, create an instance of this class.

7)Register the Class which we have created in to the Component and declare that component to the RestResourceRegistry component  nucleusRestResources property. 

8)How End Point Works .

Resources are registered with a REST resource registry that has a context root defined in the web.xml file. This context root takes the following format:


 <context-param>
    <param-name>context-root</param-name>
    <param-value>/public/v1</param-value>
  </context-param>


For example, a URI may be /public/v1. The full URI would be http://localhost:8080/public/v1.

Then the corresponding end point we have to invoke . In our Scenario of above example it is like 
http://localhost:8080/public/v1/cart/ this will change as per our Requirement. 


Try to access your First Ever JAX-RS application .

We will  start looking in to the advance concepts in the upcoming Tutorials . You can also take a look at the OOTB Classes on the JAX-RS implementation for more technical Stuffs.

Happy Learning !!!!!

2 comments:
Write comments
  1. It's nice to read article about cxml, it will definitely help me in future.
    Commerce XML?

    ReplyDelete
  2. how to call another endpoint from one endpoint for a specific response.
    e.g. have get cart endpoint from CartRestResource
    and have custom restResource, which has different business logic but at end should return response similar to get cart. can you help with it ?

    ReplyDelete