[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["難以理解","hardToUnderstand","thumb-down"],["資訊或程式碼範例有誤","incorrectInformationOrSampleCode","thumb-down"],["缺少我需要的資訊/範例","missingTheInformationSamplesINeed","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-09-05 (世界標準時間)。"],[[["\u003cp\u003eThis page details how to use properties within Java callouts in Apigee and Apigee hybrid, enabling dynamic configuration of Java code at runtime.\u003c/p\u003e\n"],["\u003cp\u003eJava callout policies in Apigee use the \u003ccode\u003e<Properties>\u003c/code\u003e element to define name/value pairs that can be accessed from the Java code.\u003c/p\u003e\n"],["\u003cp\u003eThe provided example project, available on GitHub, demonstrates how to retrieve property values specified in the policy and use them to set response headers.\u003c/p\u003e\n"],["\u003cp\u003eCompilation instructions are provided for both Maven and \u003ccode\u003ejavac\u003c/code\u003e, including steps to ensure the JAR file is in the correct directory for proxy deployment.\u003c/p\u003e\n"],["\u003cp\u003eDeploying the sample proxy allows you to call it and verify that the Java callout correctly retrieves the property value, setting a response header accordingly.\u003c/p\u003e\n"]]],[],null,["# How to use properties in the JavaCallout\n\n*This page\napplies to **Apigee** and **Apigee hybrid**.*\n\n\n*View [Apigee Edge](https://docs.apigee.com/api-platform/get-started/what-apigee-edge) documentation.*\n\nWhat is a Java callout?\n-----------------------\n\nIf you're new to JavaCallouts, we recommend that you start with [How to create a Java\ncallout](/apigee/docs/api-platform/develop/how-create-java-callout).\n\nUsing properties in JavaCallout policies\n----------------------------------------\n\nProperties let you specify name/value pairs in a JavaCallout policy that you can access from\nyour Java code at runtime. You must specify a literal string value for each property; you cannot\nreference flow variables in this element.\n\nLet's walk through a simple JavaCallout policy example that uses properties. In this example, we\ncreate a proxy with that includes a JavaCallout policy. The policy uses the\n`\u003cProperties\u003e` element to specify a name/value pair. In the Java code, we\nretrieve the value and use it to set a response header.\n\n### Download the project\n\nTo make things simple, you can download this project from the Apigee [api-platform-samples](https://github.com/apigee/api-platform-samples) repository on\nGitHub.\n\n1. Download or clone [api-platform-samples](https://github.com/apigee/api-platform-samples) to your system.\n2. In a terminal or code editor of your choice, go to the `api-platform-samples/doc-samples/java-properties` project.\n\n### The JavaCallout policy\n\nThe policy uses the `\u003cProperties\u003e` element. This element lets you specify\nname/value pairs. At runtime, your Java code can access the values of the properties specified in\nthe policy, as we'll see shortly. \n\n```java\n\u003cJavaCallout name=\"java-callout\"\u003e\n \u003cClassName\u003ecom.apigeesample.JavaProperties\u003c/ClassName\u003e\n \u003cResourceURL\u003ejava://edge-custom-policy-java-properties.jar\u003c/ResourceURL\u003e\n \u003cProperties\u003e\n \u003cProperty name=\"prop\"\u003eWORLD!\u003c/Property\u003e\n \u003c/Properties\u003e\n\u003c/JavaCallout\u003e\n```\n\n### The sample Java code\n\nThe Java code for this sample shows you how to retrieve a property that was specified in the\nJavaCallout policy. In the sample project, you can find the source code in\n`java-properties/callout/src/main/java/JavaProperties.java`. We'll walk through the\nsteps for compiling and deploying this code later in this topic. \n\n```java\npackage com.apigeesample;\n\nimport com.apigee.flow.execution.ExecutionContext;\nimport com.apigee.flow.execution.ExecutionResult;\nimport com.apigee.flow.execution.spi.Execution;\nimport com.apigee.flow.message.MessageContext;\n\nimport java.util.Map;\n\npublic class JavaProperties implements Execution {\n\n\tprivate Map \u003cString,String\u003e properties; // read-only\n\n\tpublic JavaProperties(Map \u003cString,String\u003e properties) {\n\t this.properties = properties;\n\t}\n\n\tpublic ExecutionResult execute(MessageContext messageContext, ExecutionContext executionContext) {\n\n\t\ttry {\n\n\t\t messageContext.getMessage().setHeader(\"X-PROPERTY-HELLO\", this.properties.get(\"prop\"));\n\n return ExecutionResult.SUCCESS;\n\n\t\t} catch (Exception e) {\n\t\t\treturn ExecutionResult.ABORT;\n\t\t}\n\t}\n}\n```\n| **Note:** To retrieve the properties specified in the policy, all you need to do is implement the Execution class constructor as shown above. Then, in the Java code, you can get the property value you want by name.\n\n### Compile your code with Maven\n\nThe project is set up so that you can compile with Maven. If you want to use\n`javac`, we'll include an example as well.\n| **Note:** The POM file and configuration for the Maven compile should work, but are offered in the Git repository as an example only. You may need to make adjustments for your environment. If you make changes to the sample code, you may also need to adjust the Maven configuration.\n\n1. Be sure that you have Maven installed: \n\n ```java\n mvn -version\n ```\n2. Execute the script `java-properties/buildsetup.sh`. This script installs the required JAR dependencies in your local Maven repo.\n3. cd to the `java-properties/callout` directory.\n4. Execute Maven: \n\n ```java\n mvn clean package\n ```\n | **Note:** If you get a Maven error, be sure that you are in the `java-properties/callout` directory.\n5. If you wish, verify that the JAR file `edge-custom-policy-java-properties.jar` was copied to `java-properties/apiproxy/resources/java`. This is the required location for JAR files that you wish to deploy with a proxy.\n\n### Compile with javac (optional)\n\nIf you want to use `javac` to compile the code, you can do something similar to the\nfollowing (from the `java-properties` directory). The required JAR files are provided\nfor you in the `java-properties/lib` directory.\n\n1. cd to `api-platform-samples/doc-samples/java-properties`.\n2. Be sure you have javac in your path. \n\n ```java\n javac -version\n ```\n3. Execute the following javac command: \n\n ```java\n javac -d . -classpath ./lib/expressions-1.0.0.jar:./lib/message-flow-1.0.0.jar:. callout/src/main/java/JavaProperties.java\n ```\n4. Create a JAR file: \n\n `jar -cvf edge-custom-policy-java-properties.jar\n ./com/apigeesample/JavaProperties.class` \n5. Copy the JAR file to the apiproxy/resources/java directory. This is the required location for JAR files that you wish to deploy with a proxy. \n\n ```java\n cp edge-custom-policy-java-properties.jar apiproxy/resources/java\n ```\n\n### Deploy and call the proxy\n\nA deploy script is provided in the `./java-properties` directory. But before you\nrun it, you need to do a quick setup.\n\n1. cd to `api-platform-samples/doc-samples/java-properties`\n2. The simplest way to deploy the proxy is to bundle it in a zip file and upload the proxy bundle to an environment in your Apigee organization. See [Creating an API proxy](/apigee/docs/api-platform/develop/ui-create-proxy). Be sure to use the **Upload Proxy Bundle** option. See also [Tips and tricks for uploading API proxy in a proxy bundle](https://community.apigee.com/questions/64498/tips-and-tricks-for-uploading-api-proxy-in-proxy-b.html) in the Apigee community.\n3. When the proxy is deployed, try calling it: \n\n ```java\n curl https://$HOSTNAME/java-properties\n ```\n\n The proxy returns the header: `X-PROPERTY-HELLO: WORLD!`. Remember that in the\n policy, we added a property name/value pair `\"prop/WORLD!\"`. The JavaCallout policy\n retrieves the value `\"WORLD!\"` and sets it in a header called\n `X-PROPERTY-HELLO`: \n\n ```java\n messageContext.getMessage().setHeader(\"X-PROPERTY-HELLO\", this.properties.get(\"prop\"));\n ```"]]