Note: This API is deprecated as of Fabric v2.5. When developing applications for Hyperledger Fabric v2.4 and later, you should use the Fabric Gateway client API.
The Fabric Gateway SDK allows applications to interact with a Fabric blockchain network. It provides a simple API to submit transactions to a ledger or query the contents of a ledger with minimal code.
The Gateway SDK implements the Fabric programming model as described in the Developing Applications chapter of the Fabric documentation.
The following shows a complete code sample of how to connect to a fabric network, submit a transaction and query the ledger state using an instantiated smart contract (fabcar sample).
importjava.io.IOException;
importjava.nio.charset.StandardCharsets;
importjava.nio.file.Path;
importjava.nio.file.Paths;
importjava.util.concurrent.TimeoutException;
importorg.hyperledger.fabric.gateway.Contract;
importorg.hyperledger.fabric.gateway.ContractException;
importorg.hyperledger.fabric.gateway.Gateway;
importorg.hyperledger.fabric.gateway.Network;
importorg.hyperledger.fabric.gateway.Wallet;
importorg.hyperledger.fabric.gateway.Wallets;
classSample {
publicstaticvoidmain(String[] args) throwsIOException {
// Load an existing wallet holding identities used to access the network.PathwalletDirectory = Paths.get("wallet");
Walletwallet = Wallets.newFileSystemWallet(walletDirectory);
// Path to a common connection profile describing the network.PathnetworkConfigFile = Paths.get("connection.json");
// Configure the gateway connection used to access the network.Gateway.Builderbuilder = Gateway.createBuilder()
.identity(wallet, "user1")
.networkConfig(networkConfigFile);
// Create a gateway connectiontry (Gatewaygateway = builder.connect()) {
// Obtain a smart contract deployed on the network.Networknetwork = gateway.getNetwork("mychannel");
Contractcontract = network.getContract("fabcar");
// Submit transactions that store state to the ledger.byte[] createCarResult = contract.createTransaction("createCar")
.submit("CAR10", "VW", "Polo", "Grey", "Mary");
System.out.println(newString(createCarResult, StandardCharsets.UTF_8));
// Evaluate transactions that query state from the ledger.byte[] queryAllCarsResult = contract.evaluateTransaction("queryAllCars");
System.out.println(newString(queryAllCarsResult, StandardCharsets.UTF_8));
} catch (ContractException | TimeoutException | InterruptedExceptione) {
e.printStackTrace();
}
}
}Full Javadoc documentation is published for each of the following versions:
Add the following dependency to your project's pom.xml file:
<dependency>
<groupId>org.hyperledger.fabric</groupId>
<artifactId>fabric-gateway-java</artifactId>
<version>2.2.0</version>
</dependency>Add the following dependency to your project's build.gradle file:
implementation 'org.hyperledger.fabric:fabric-gateway-java:2.2.0'The following table shows versions of Fabric, Java and other dependencies that are explicitly tested and that are supported for use with version 2.2 of the Fabric Gateway SDK. Refer to the appropriate GitHub branch for compatibility of other release versions.
| Tested | Supported | |
|---|---|---|
| Fabric | 2.2 | 2.2 |
| Java | 8, 11, 17 | 8+ |
| Platform | Ubuntu 22.04 |
To run Java SDK clients on IBM POWER systems (e.g. zLinux), use Java 11 and set the SSL provider to ‘JDK’ by either supplying the -D command line option:
-Dorg.hyperledger.fabric.sdk.connections.ssl.sslProvider=JDK
or with the environment variable set as follows:
ORG_HYPERLEDGER_FABRIC_SDK_CONNECTIONS_SSL_SSLPROVIDER=JDK
git clone https://github.com/hyperledger/fabric-gateway-java.git
cd fabric-gateway-java
mvn install
The mvn install command will download the dependencies and run all the unit tests and scenario tests. It will also generate all the crypto material required by these tests.
Docker is required to run the scenario tests.
All classes and methods have a high coverage (~90%) of unit tests. These are written using the JUnit, AssertJ and Mockito frameworks.
Scenario tests are written using the Cucumber BDD framework.