Create Product Resource and First Method

Create Product Resource and First Method

  1. Create the method createProductsResource with two parameters: RestApi and ApiStackProps, and initialize it within the constructor.

    private void createProductsResource(RestApi restApi, ApiStackProps apiStackProps) {
    }
    
![Architect](/images/7/2/01.png?featherlight=false&width=60pc)

2. Create a new resource in API Gateway with the path **/products`. Using `restApi.getRoot().addResource("products")** will create a new sub-resource with the path **/products** from the root of the API (`restApi`).

```java
//products
Resource productsResource = restApi.getRoot().addResource("products");

Architect

  1. Add a GET method to productsResource using the addMethod method.
productsResource.addMethod("GET");

Architect

  1. Next, we define an Integration to handle GET requests to the endpoint /products.
   productsResource.addMethod("GET", new Integration(
                   IntegrationProps.builder()
                           .build()));

Architect

  1. Then, specify the Integration type as HTTP_PROXY, which means using an HTTP proxy to forward requests, and choose the Integration method type as GET.
   .type(IntegrationType.HTTP_PROXY)
   .integrationHttpMethod("GET")

Architect

  1. Set the URI of the backend that the API Gateway will forward requests to. Here, the DNS name of the Network Load Balancer is used to construct the URL, along with port 8081 and the API path /api/products being called.
.uri("http://" + apiStackProps.networkLoadBalancer().getLoadBalancerDnsName() + ":8081/api/products")

Architect

  1. Next, use options to further configure the IntegrationOptions.
   .options(IntegrationOptions.builder()
           .build())

Architect

  1. Specify the VpcLink to identify which VPC connection will be used for the Integration method and define the connection type as VPC_LINK, meaning connecting through an AWS VPC.
.vpcLink(apiStackProps.vpcLink())
.connectionType(ConnectionType.VPC_LINK)

Architect