Was this helpful?

Apigee Edge provides a range of out-of-the-box policy capabilities that address common API management requirements. However, there are some cases where your API requires custom behavior that is not implemented in the out-of-the-box policy palette. In these cases, Apigee provides several options that enable developers to script or code customized API behavior. One approach is to implement the desired behavior in the Java programming language.

Note: You must be an Apigee Enterprise customer to use the JavaCallout policy.

For lightweight operations, such as API calls to remote services, the ServiceCallout policy is recommended. See Call services or APIs using ServiceCallout.

For relatively simple interaction with message content, such as modifying or extracting HTTP headers, parameters, or message content, you can use JavaScript or Python languages.

For a detailed code example, see Implementing HTTP clients in JavaScript.

JavaDocs

To get started writing Java for Apigee Edge, refer to the JavaCallout Javadocs.

JavaCallout Samples

For commented sample code, see the JavaCallout samples repository.

Compiling, deploying, testing

JavaCallouts depend on two JAR files that are available in Apigee Edge samples repository on Github. For instructions on compiling and deploying, see the instructions provided in the JavaCallout overview.

Configuring a JavaCallout policy

A JavaCallout policy references a main JAR file. The referenced JAR file contains compiled Java code written to the Java API exposed by the API Platform processing pipeline. This JAR must be placed in an API proxy under /resources/java. 

If your JavaCallout code 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, you can 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. 

Configure the JavaCallout policy using the following elements.

Field Name Description
ResourceURL Specifies the Java resource that is the name of the JAR file). Note that only resource of type java is allowed
ClassName Specifies the name of the Java class that executes the callout

Example - Java Callout policy

In the example below, the element, ResourceURL specifies the name of the relevant JAR file.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<JavaCallout name="MyJavaCalloutPolicy">
        <ResourceURL>java://MyJavaCallout.jar</ResourceURL>
        <ClassName>com.example.mypolicy.MyJavaCallout</ClassName>
</JavaCallout>

Policy-specific error codes

The default format for error codes returned by Policies is:

{
  "code" : " {ErrorCode} ",
  "message" : " {Error message} ",
  "contexts" : [ ]
}

The JavaCallout Policy type defines the following error codes:

Error Code Message
JavaCalloutInstantiationFailed Failed to instantiate the JavaCallout Class {0}
NoResourceForURL Could not locate a resource with URL {0}
NoAppropriateConstructor No appropriate constructor found in JavaCallout class {0}
JavaClassNotFoundInJavaResource Failed to find the ClassName in java resource {0} - {1}
JavaClassDefinitionNotFound Failed to load java class {0} definition

Policy schema

Each policy type is defined by an XML schema (.xsd). For reference, policy schemas are available on GitHub.

コメント

The Link to the JavaDocs on Github doesn't work anymore!!! Please fix it

HI Christoph,
Here is the direct link:
https://github.com/apigee/api-platform-samples/tree/2a78e63da3aa5b3c98275cb902d5b210834e4482/sample-proxies/java-callout/javadocs-javacallout

Thanks

コメントを追加

Provide your email address if you wish to be contacted offline about your comment.
We will not display your email address as part of your comment.

We'd love your feedback and perspective! Please be as specific as possible.
Type the characters you see in this picture. (verify using audio)

Type the characters you see in the picture above; if you can't read them, submit the form and a new image will be generated. Not case sensitive.