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.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());
        out.flush();
        out.close();
    }
}          
  • 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.

HTTP triggers support conventional web applications

Conventional Servlet-based web applications can be easily migrated to the Function Compute platform. The platform currently supports mainstream frameworks such as Spring, SpringBoot, and Struts2. The following example shows how to load a web application by using the fc-java-common library provided in Function Compute.

  1. Package your web project to generate a package named demo.war.
  2. Upload the demo.war package to OSS, for example, to the demo-bucket directory.
  3. Create a function and specify an initializer function and a handler function for the function.
    • The sample code of the function is as follows:

      package com.aliyun.fc.example;
      
      import java.io.IOException;
      
      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.FcAppLoader;
      import com.aliyun.fc.runtime.FunctionComputeLogger;
      import com.aliyun.fc.runtime.FunctionInitializer;
      import com.aliyun.fc.runtime.HttpRequestHandler;
      
      public class HelloWeb implements FunctionInitializer, HttpRequestHandler {
        private FcAppLoader fcAppLoader = new FcAppLoader();
      
        private String ossEndPoint = "YourOSSEndPoint";
        private String bucket = "YourOSSBucket";
        private String key = "YourWarName";
        private String userContextPath = "/2016-08-15/proxy/{YourServiceName}/{YourFunctionName}";
      
        @Override
        public void initialize(Context context) throws IOException {
              FunctionComputeLogger fcLogger = context.getLogger();
      
              fcAppLoader.setFCContext(context);
      
              // Load code from OSS
              fcAppLoader.loadCodeFromOSS(ossEndPoint, bucket, key);
      
              // Init webapp from code
              fcAppLoader.initApp(userContextPath, HelloWeb.class.getClassLoader());
        }
      
        @Override
        public void handleRequest(HttpServletRequest request, HttpServletResponse response, Context context)
                  throws IOException, ServletException {
              try {
                  fcAppLoader.forward(request, response);
              } catch (Exception e) {
                  e.printStackTrace();
              }
        }
      }                            
    • Reference the Maven repository.

       <dependency>
            <groupId>com.aliyun.fc.runtime</groupId>
            <artifactId>fc-java-core</artifactId>
            <version>1.3.0</version>
        </dependency>
      
        <dependency>
            <groupId>com.aliyun.fc.runtime</groupId>
            <artifactId>fc-java-common</artifactId>
            <version>2.2.2</version>
        </dependency>                  

More information

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