You can use HTTP handlers to efficiently process HTTP requests. When you invoke a function, Function Compute runs the handler that you specify in the function code to process HTTP requests. This topic describes the structure and characteristics of HTTP handlers for Java.

Signatures for HTTP handlers

Function Compute provides a Servlet-based HTTP function handler in the following format:

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;
}      

To use HTTP handlers, you must configure an HTTP trigger for the function. To use an HTTP trigger, you must upgrade the fc-java-core library 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>

Sample code

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

    In Function Compute, the interface for HTTP triggers is based on the standard Servlet protocol. Your requests are encapsulated into the HttpServletRequest object. You can obtain request parameters and the header from this object. In addition, Function Compute pre-encapsulates some attributes into HttpServletRequest. You can use getAttribute to query these attributes. The following items are included:

    • FC_REQUEST_PATH: obtains the path of the request.

    • FC_REQUEST_URI: obtains the uniform resource identifier (URI) of the request.

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

  • HttpServletResponse

    You can use the standard HttpServletResponse object to return the header and body of the response.

  • context

    The context parameter contains the runtime information of a function, such as the request ID and the temporary AccessKey pair. This parameter is of the com.aliyun.fc.runtime.Context type.

Sample programs

The Function Compute libraries contain sample programs that use various handler types and interfaces. Each sample program contains methods for easy compilation and deployment. Examples: