When you use Java for programming in Function Compute, you must define a Java function as the handler function. After you configure an HTTP trigger for a function, the function can directly process HTTP requests.

HTTP function handler

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 an HTTP trigger, you must upgrade the version of the fc-java-core library to 1.3.0 or later.

Sample code of an HTTP trigger

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.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();
  • HttpServletRequest

    In Function Compute, the API operations for HTTP triggers are based on the standard Servlet protocol. Your request is encapsulated into the HttpServletRequest object. You can obtain request parameters and header from this object. In addition, Function Compute encapsulates some attributes into the HttpServletRequest object. You can call the getAttribute method to query these attributes. The attributes are described as follows:

    • 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 parameter

    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.

More information

For more information about how to use an HTTP trigger, see .