You can use HTTP handlers to efficiently process HTTP requests. When the function is invoked, it Function Compute run the execution method you provided to handle the HTTP request. This article describes the structure and features of Java HTTP Handler.

HTTP Handler Signature

The Function Compute provides an HTTP entry based on the Servlet protocol in the following form.

public interface HttpRequestHandler {
    /**
     * The entrance function of fc http trigger 
     * @param request The servlet request
     * @param response The servlet response
     * @param context The fc context
     * @throws IOException If IO exception happened
     * @throws ServletException If servlet exception happened
     */
    public void handleRequest(HttpServletRequest request, HttpServletResponse response, Context context) throws IOException, ServletException;
}      

The HTTP request handler requires you to configure an HTTP trigger for a function. To use an HTTP trigger, you must upgrade the fc-java-core library version to 1.3.0 or later. Example: For more information about HTTP triggers, see Overview.

<dependency>
  <groupId>com.aliyun.fc.runtime</groupId>
  <artifactId>fc-java-core</artifactId>
  <version>1.4.1</version>
</dependency>

Simple example

package com.aliyun.fc.example;

import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.aliyun.fc.runtime.Context;
import com.aliyun.fc.runtime.HttpRequestHandler;

public class Hello implements HttpRequestHandler {
    public void handleRequest(HttpServletRequest request, HttpServletResponse response, Context context)
            throws IOException, ServletException {
        String requestPath = (String) request.getAttribute("FC_REQUEST_PATH");
        String requestURI = (String) request.getAttribute("FC_REQUEST_URI");
        String requestClientIP = (String) request.getAttribute("FC_REQUEST_CLIENT_IP"); 

        response.setStatus(200);
        response.setHeader("header1", "value1");
        response.setHeader("header2", "value2");

        String body = String.format("Path: %s\n Uri: %s\n IP: %s\n", requestPath, requestURI, requestClientIP);
        OutputStream out = response.getOutputStream();
        out.write((body).getBytes());
    }
}          
  • HttpServletRequest

    The interface of the Function Compute HTTP trigger uses the standard Servlet protocol. Your request is encapsulated into a HttpServletRequest object, which can be obtained through request parameters and first-class requests. In addition, Function Compute preencapsulates some attributes in the HttpServletRequest, you can obtain them by getAttribute methods, including the following:

    • FC_REQUEST_PATH: obtains the path of the request.

    • FC_REQUEST_URI: gets the URI of the request.

    • FC_REQUEST_CLIENT_IP: obtains the client IP address of the request.

  • HttpServletResponse

    You can use standard HttpServletResponse protocol objects to return response headers and response bodies.

  • context

    The context parameter contains the run time information of some functions, such as the request ID and temporary AccessKey, and its type is com.aliyun.fc.runtime.Context.

Sample code

Function Compute official library contains sample applications that use various handler types and interfaces. Each sample application contains methods for easy compilation and deployment. Examples: