Enables you to use Java to implement custom behavior that is not included out-of-the-box by Apigee policies. In your Java code, you can access message properties (headers, query parameters, content) and flow variables in the proxy flow. If you're just getting started with this policy, see How to create a Java callout.
Supported Java versions include: Oracle JDK 11 and OpenJDK 11
This policy is an Extensible policy and use of this policy might have cost or utilization implications, depending on your Apigee license. For information on policy types and usage implications, see Policy types.
For guidelines, see "When should I use a Java callout?" in How to create a Java callout.
The Java Callout policy lets you get and set flow variables, execute custom logic and perform error handling, extract data from requests or responses and more. This policy allows you to implement custom behavior that is not covered by any other standard Apigee policies.
You can package your Java application with whatever package JAR files you need. Note that there are some restrictions on what you can do with a Java Callout. These are listed below in Restrictions.The policy's <Property>
element let's you specify a name/value pair that you can retrieve at runtime in your Java code. For a working example that uses properties, see How to use properties in a Java callout.
Use the <Property> element's name
attribute to specify the name with which to access the property from Java code. The <Property>
element's value (the value between the opening and closing tags) is the value that will be received by the Java code. The value must be a string; you cannot reference a flow variable to get the value.
response.status.code
. <JavaCalloutasync="false"continueOnError="false"enabled="true"name="JavaCallout"> <DisplayName>JavaCallout</DisplayName> <ClassName>com.example.mypolicy.MyJavaCallout</ClassName> <ResourceURL>java://MyJavaCallout.jar</ResourceURL><Properties> <Propertyname="source">response.status.code</Property> </Properties> </Javascript>
publicclassMyJavaCalloutimplementsExecution{publicMyJavaCallout(Map<string,string>props){// Extract property values from map.}...}
For a clear description of how to set variables in the message context (flow variables) in your Java code, see this Apigee Community post.
The element reference describes the elements and attributes of the JavaCallout policy.
<JavaCalloutname="MyJavaCalloutPolicy"> <ClassName>com.example.mypolicy.MyJavaCallout</ClassName> <ResourceURL>java://MyJavaCallout.jar</ResourceURL> </JavaCallout>
<JavaCalloutname="MyJavaCalloutPolicy"enabled="true"continueOnError="false"async="false">
The following table describes attributes that are common to all policy parent elements:
Attribute | Description | Default | Presence |
---|---|---|---|
name | The internal name of the policy. The value of the Optionally, use the | N/A | Required |
continueOnError | Set to Set to | false | Optional |
enabled | Set to Set to | true | Optional |
async | This attribute is deprecated. | false | Deprecated |
Use in addition to the name
attribute to label the policy in the management UI proxy editor with a different, natural-language name.
<DisplayName>PolicyDisplayName</DisplayName>
Default | N/A If you omit this element, the value of the policy's |
---|---|
Presence | Optional |
Type | String |
Specifies the name of the Java class that executes when the Java Callout policy runs. The class must be included in the JAR file specified by the <ResourceURL>
. See also How to create a Java callout.
<JavaCalloutname="MyJavaCalloutPolicy"> <ResourceURL>java://MyJavaCallout.jar</ResourceURL><ClassName>com.example.mypolicy.MyJavaCallout</ClassName> </JavaCallout>
Default: | N/A |
Presence: | Required |
Type: | String |
Adds new properties that you can access from Java code at runtime.
<Properties> <Propertyname="propName">propertyValue</Property> </Properties>
Default: | None |
Presence: | Optional |
Type: | String |
Specifies a property you can access from Java code at runtime. You must specify a literal string value for each property; you cannot reference flow variables in this element. For a working example that uses properties, see How to use properties in a Java callout.
<Properties> <Propertyname="propName">propertyValue</Property> </Properties>
Default: | None |
Presence: | Optional |
Type: | String |
Attribute | Description | Default | Presence |
---|---|---|---|
name | Specifies the name of the property. | N/A | Required. |
This element specifies the Java JAR file that will execute when the Java callout policy runs.
You can store this file at the API proxy scope (under /apiproxy/resources/java
in the API proxy bundle or in the Scripts section of the API proxy editor's Navigator pane), or at the organization or environment scopes for reuse across multiple API proxies, as described in Resource files.
<JavaCalloutname="MyJavaCalloutPolicy"> <ResourceURL>java://MyJavaCallout.jar</ResourceURL><ClassName>com.example.mypolicy.MyJavaCallout</ClassName> </JavaCallout>
Default: | None |
Presence: | Required |
Type: | String |
This section describes the fault codes and error messages that are returned and fault variables that are set by Apigee when this policy triggers an error. This information is important to know if you are developing fault rules to handle faults. To learn more, see What you need to know about policy errors and Handling faults.
These errors can occur when the policy executes.
Fault code | HTTP status | Cause | Fix |
---|---|---|---|
steps.javacallout.ExecutionError | 500 | Occurs when Java code throws an exception or returns null during the execution of a JavaCallout policy . | build |
These errors can occur when the proxy containing the policy is deployed.
Error name | Fault string | HTTP status | Occurs when |
---|---|---|---|
ResourceDoesNotExist | Resource with name [name] and type [type] does not exist | N/A | The file specified in the <ResourceURL> element does not exist. |
JavaCalloutInstantiationFailed | Failed to instantiate the JavaCallout Class [classname] | N/A | The class file specified in the <ClassName> element is not in the jar. |
IncompatibleJavaVersion | Failed to load java class [classname] definition due to - [reason] | N/A | See fault string. Supported Java versions include: Oracle JDK 7/8 and OpenJDK 7/8 |
JavaClassNotFoundInJavaResource | Failed to find the ClassName in java resource [jar_name] - [class_name] | N/A | See fault string. |
JavaClassDefinitionNotFound | Failed to load java class [class_name] definition due to - [reason] | N/A | See fault string. |
NoAppropriateConstructor | No appropriate constructor found in JavaCallout class [class_name] | N/A | See fault string. |
NoResourceForURL | Could not locate a resource with URL [string] | N/A | See fault string. |
These variables are set when this policy triggers an error. For more information, see What you need to know about policy errors.
Variables | Where | Example |
---|---|---|
fault.name="fault_name" | fault_name is the name of the fault, as listed in the Runtime errors table above. The fault name is the last part of the fault code. | fault.name Matches "ExecutionError" |
javacallout.policy_name.failed | policy_name is the user-specified name of the policy that threw the fault. | javacallout.JC-GetUserData.failed = true |
{"fault":{"faultstring":"Failed to execute JavaCallout. [policy_name]","detail":{"errorcode":"javacallout.ExecutionError"}}}
<FaultRulename="JavaCalloutFailed"> <Step> <Name>AM-JavaCalloutError</Name> </Step> <Condition>(fault.nameMatches"ExecutionError")</Condition> </FaultRule>
For details on how to compile your custom Java code and deploy it with a proxy, see How to create a Java callout.
Below are restrictions that you need to consider when writing Java Callouts:
io.apigee
or com.apigee
as package names in Java Callouts. Those names are reserved and used by other Apigee modules.Place the JAR in an API proxy under /resources/java
. If your Java Callout relies on additional third-party libraries packaged as independent JAR files, then place those JAR files in the /resources/java
directory as well to ensure that they are loaded correctly at runtime.
If you are using the management UI to create or modify the proxy, add a new resource and specify an additional dependent JAR file. If there are multiple JARs, simply add them as additional resources. You do not need to modify the policy configuration to refer to additional JAR files. Putting them in /resources/java
is sufficient.
For information on uploading Java JARs, see Resource files.
For a detailed example that demonstrates how to package and deploy a Java Callout using Maven or javac, see How to create a Java callout.
Javadoc for writing Java Callout code is included here on GitHub. You will need to clone or download the HTML to your system, and then simply open the index.html file in a browser.
Having multiple copies of same JAR deployed can cause non-deterministic behavior at runtime because of potential ClassLoader conflicts.
/resources/java
directory in API proxies that you develop locally.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 2025-04-24 UTC.