Wednesday, May 2, 2018

ATG JAX-RS Creating End Point

The complete code can be found here:
https://github.com/theshaik/ATGJAXRSServices


Follow below steps to create new JAX-RS rest end point.


1. Create a Java class UserRestResource.jave

package com.demo.restresources;

package com.demo.restresources;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;

import com.demo.model.User;

import atg.nucleus.GenericService;
import atg.service.jaxrs.RepresentationModel;
import atg.service.jaxrs.RestException;
import atg.service.jaxrs.annotation.Endpoint;
import atg.service.jaxrs.annotation.RestResource;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@RestResource(id = "com.demo.restresource")
@Api(value = "Demo Endpoints")
@Path("/demo")
public class UserRestResource extends GenericService {

    @GET
    @Path(value = "/users/{userId}")
    @Endpoint(id = "/demo/users/{userId}#GET", isSingular=true, filterId="demo.user-default")
    @ApiOperation(value = "Returns a specific user's details.")
    public RepresentationModel getUser(@ApiParam(required = true) @PathParam(value = "userId") String pUserId) throws RestException {
        if (this.isLoggingInfo()) {
            this.logInfo("Entered getUser endpoint, user Id : " + pUserId);
        }
        User user = new User();
        user.setId(pUserId);
        user.setName("Doe");
        return new RepresentationModel.Builder().state(user).build();
    }
}


2. Create the model class User.java

package com.demo.model;

public class User {

    private String id;
    private String name;
   
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
   
}


3. Create component properties file
#com.demo.restresources.UserRestResources.properties
$class=com.demo.restresources.UserRestResource
$classloader=/atg/dynamo/service/jaxrs/JerseyClassLoaderService


4. Register the nuclues component
#/atg/dynamo/service/jaxrs/RestResourceRegistry.properties
nucleusRestResources+=\
    /com/demo/restresources/UserRestResource

5. Create the Payload schema filter
#/atg/dynamo/service/payloadschema/payloadSchema.xml
<?xml version="1.0" encoding="UTF-8"?>
<payload-schemas xml-combine="append">
    <schema id="demo.user-default">
        <untyped-bean/>
        <property name="id" />
        <property name="name" />
    </schema>
</payload-schemas>


6. Register the resource and classloader
#/atg/dynamo/service/jaxrs/JerseyClassLoaderService.properties
childFirstPrefixes+=com/demo/restresources
classpathFiles+=\
  {appModuleResource?moduleID=MyModule&resourceURI=lib/demo-jersey-classloader.jar}


7. Update the ant script to create the class loader
    <target name="demo-jerseyclassloader-jar">
      <!-- Make a jar file with misc override classes. -->
        <jar
            jarfile="lib/demo-jersey-classloader.jar"
            basedir="bin"
            includes="
            com/demo/restresources/UserRestResource.class">
        </jar>
    </target>

8. Include the above ant target as part of build process


No comments:

Post a Comment