View
Apigee Edge documentation.
Apigee provides a range of policies that address common API management requirements such as security, data transformation, traffic management, and others.
However, there are some cases where your API requires custom behavior that is not implemented in a standard policy. In these cases, Apigee provides several options that enable you to script or code customized API behavior. One approach is to implement the desired behavior in Java.
Supported Java versions include: Oracle JDK 11 and OpenJDK 11.
The JavaCallout policy lets you call Java code from within an executing proxy flow. Your Java code needs to implement certain Apigee-specific Java interfaces that allow the code to interact with the executing proxy. For example, Java methods exist for getting and setting headers, query parameters, flow variables, and other entities within the current flow context of the proxy.
Let's look at situations where the JavaCallout policy is useful, and situations where you should consider other approaches.
Before using the JavaCallout policy, note that there may be alternative approaches that you can use instead. For example:
The JavaCallout policy supports these basic operations:
Most system calls are disallowed. You cannot:
expressions-1.0.0.jar and message-flow-1.0.0.jar.Although some such calls may work, they are unsupported and liable to be actively disabled at any time. Avoid making such calls in your code.
Do not use or rely on Java libraries that are included with Apigee. Those libraries are for Apigee product functionality only, and there's no guarantee that a library will be available from release to release. If you use such libraries, use them in non-production demonstrations only.
Let's go through a basic hello world a JavaCallout policy example. In this example, we create a simple proxy with JavaCallout that returns a "hello world" response. The proxy can return one of two possible responses:
Hello, <name>!
"Hello, Guest!"To make things simple, we have a basic project prepared for you on GitHub in the Apigee api-platform-samples repository.
api-platform-samples on your system, do a pull to make sure
you have the latest version.api-platform-samples/doc-samples/java-hello project.java-hello/callout/src/main/java/HelloJava.java.
This file is a skeleton version of the main Java class that we will implement. The imported
packages are required for Apigee JavaCallout code. These classes provide methods that allow you
to access the proxy execution context. We'll walk through the steps for compiling and deploying
this code shortly.package com.apigeesample; import com.apigee.flow.execution.ExecutionContext; import com.apigee.flow.execution.ExecutionResult; import com.apigee.flow.execution.spi.Execution; import com.apigee.flow.message.MessageContext; public class HelloJava implements Execution { public ExecutionResult execute(MessageContext messageContext, ExecutionContext executionContext) { try { // Your code here. return ExecutionResult.SUCCESS; } catch (Exception e) { return ExecutionResult.ABORT; } } }
// Your code here with the following code:String name = messageContext.getMessage().getHeader("username"); if (name != null && name.length()>0) { messageContext.getMessage().setContent("Hello, " + name + "!"); messageContext.getMessage().removeHeader("username"); } else { messageContext.getMessage().setContent("Hello, Guest!"); }
mvn -version
pom.xml file to downoad the required JAR dependencies from
Artifact Registry:
<repositories> <repository> <id>artifact-registry</id> <url>https://us-maven.pkg.dev/apigee-release/apigee-java-callout-dependencies</url> </repository> </repositories> <dependencies> <dependency> <groupId>com.apigee.gateway.libraries</groupId> <artifactId>message-flow</artifactId> <version>1.0.0</version> <scope>compile</scope> <type>jar</type> </dependency> <dependency> <groupId>com.apigee.infra.libraries</groupId> <artifactId>expressions</artifactId> <version>1.0.0</version> <scope>compile</scope> <type>jar</type> </dependency> </dependencies>
curl calls to download the required JAR dependencies from
Artifact Registry:
curl "https://us-maven.pkg.dev/apigee-release/apigee-java-callout-dependencies/com/apigee/gateway/libraries/message-flow/1.0.0/message-flow-1.0.0.jar" -v -L -o message-flow-1.0-0.jar
curl "https://us-maven.pkg.dev/apigee-release/apigee-java-callout-dependencies/com/apigee/infra/libraries/expressions/1.0.0/expressions-1.0.0.jar" -v -L -o expressions-1.0.0.jar
java-hello/buildsetup.sh. This script downloads the required JAR
dependencies from the Apigee GitHub repository.java-hello/callout directory.mvn clean package
edge-custom-policy-java-hello.jar was
copied to java-hello/apiproxy/resources/java. This is the required location for
JAR files that you wish to deploy with a proxy.Follow these steps to deploy and test the API proxy:
java-hello directory.
zip apiproxy-bundle.zip -r apiproxy -x \*.\*~
curl https://$HOSTNAME/java-hello -H "username:Will"
Which returns "Hello, Will!
Let's quickly examine the policies used in this proxy. Pay attention to where the policies are positioned in the proxy flow and why.
The Assign message policy
An Assign message policy is attached to the ProxyEndpoint request flow. It copies the username header from the request and assigns it to the response. This operation allows the JavaCallout policy, which is attached to the response flow, to access the username header and build a custom response body using the value of that header.
<AssignMessage async="false" continueOnError="false" enabled="true" name="CopyHeader"> <DisplayName>CopyHeader</DisplayName> <Copy source="request"> <Headers> <Header name="username"/> </Headers> </Copy> <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables> <AssignTo createNew="false" transport="http" type="response"/> </AssignMessage>
The JavaCallout policy
The JavaCallout policy is attached to the response flow. This is because the
custom Java code makes changes to the response headers and message. The policy's ClassName
element specifies the main class that is executed by the policy. The ResourceURL element is the
name of the JAR file that you built and added to the resources/java directory of the
proxy.
<JavaCallout name="hello-java"> <ClassName>com.apigeesample.HelloJava</ClassName> <ResourceURL>java://edge-custom-policy-java-hello.jar</ResourceURL> </JavaCallout>
Important things to note about implementing the JavaCallout policy are:
com.apigee.flow.execution and
com.apigee.flow.message packages. These packages must be included in the JAR file
that is packaged and deployed. You can upload your Java JAR through the Management UI proxy
editor, or you can include it in the /resources/java directory in API proxies that
you develop locally./resources/java directory as well to
ensure that they are loaded correctly at runtime./resources/java is sufficient.Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2026-06-09 UTC.