英文:
While trying to integrate the Hyperledger fabric SDK with the Spring boot getting the following error
问题
我正在尝试将Hyperledger Fabric SDK集成到Spring Boot中,当我运行应用程序时,我遇到以下错误:
java.lang.UnsupportedOperationException: null
at javax.json.spi.JsonProvider.createObjectBuilder(JsonProvider.java:281) ~[javax.json-1.1.4.jar:1.1.4]
at javax.json.Json.createObjectBuilder(Json.java:327) ~[javax.json-1.1.4.jar:1.1.4]
at org.hyperledger.fabric.sdk.NetworkConfig.fromYamlStream(NetworkConfig.java:342) ~[fabric-sdk-java-1.4.8.jar:na]
at org.hyperledger.fabric.gateway.impl.GatewayImpl$Builder.networkConfig(GatewayImpl.java:99) ~[fabric-gateway-java-1.4.3.jar:na]
at org.hyperledger.fabric.gateway.impl.GatewayImpl$Builder.networkConfig(GatewayImpl.java:88) ~[fabric-gateway-java-1.4.3.jar:na]
at org.hyperledger.fabric.gateway.impl.GatewayImpl$Builder.networkConfig(GatewayImpl.java:69) ~[fabric-gateway-java-1.4.3.jar:na]
at com.person.fabricclient.controllers.PersonController.getPerson(PersonController.java:69) ~[classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_242]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_242]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_242]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_242]
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190) ~[spring-web-5.2.5.RELEASE.jar:5.2.5.RELEASE]
以下是REST控制器代码:
@RequestMapping(value = "/hello/createperson", method = RequestMethod.GET)
public String getPerson() throws IOException {
Gateway.Builder builder = Gateway.createBuilder();
try {
// A wallet stores a collection of identities
Path walletPath = Paths.get("/home/Fabric-samples_1.4/fabric-samples/commercial-paper/organization/magnetocorp/identity/user/isabella/wallet");
Wallet wallet = Wallet.createFileSystemWallet(walletPath);
String userName = "User1@org1.example.com";
Path connectionProfile = Paths.get("/home/Fabric-samples_1.4/fabric-samples/commercial-paper/organization/magnetocorp/gateway/networkConnection.yaml");
// Set connection options on the gateway builder
builder.identity(wallet, userName).networkConfig(connectionProfile).discovery(false);
// Connect to gateway using application specified parameters
try (Gateway gateway = builder.connect()) {
// Access PaperNet network
System.out.println("Use network channel: mychannel.");
Network network = gateway.getNetwork("mychannel");
// Get addressability to commercial paper contract
System.out.println("Use org.papernet.commercialpaper smart contract.");
Contract contract = network.getContract("person", "org.go.smartcontract");
// Issue commercial paper
System.out.println("Submit commercial paper issue transaction.");
byte[] response = contract.submitTransaction("createCar", "go", "Hello", "2020-05-31");
// Process response
System.out.println("Process issue transaction response.");
// CommercialPaper paper = CommercialPaper.deserialize(response);
// System.out.println(paper);
} catch (TimeoutException e) {
e.printStackTrace();
} catch (ContractException e) {
e.printStackTrace();
}
} catch (InterruptedException | IOException e) {
e.printStackTrace();
System.exit(-1);
}
return "created";
}
当我尝试调试问题时,出现在以下类中,当我在Spring Boot中运行时,服务加载程序返回一个对象,但是当我尝试不使用Spring Boot时,服务加载程序不返回任何对象,并且在以下类中执行了此行:
Class<?> clazz = Class.forName("org.glassfish.json.JsonProviderImpl");
在以下类中:
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package javax.json.spi;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.ServiceLoader;
import javax.json.JsonArray;
import javax.json.JsonArrayBuilder;
import javax.json.JsonBuilderFactory;
import javax.json.JsonException;
import javax.json.JsonMergePatch;
import javax.json.JsonNumber;
import javax.json.JsonObject;
import javax.json.JsonObjectBuilder;
import javax.json.JsonPatch;
import javax.json.JsonPatchBuilder;
import javax.json.JsonPointer;
import javax.json.JsonReader;
import javax.json.JsonReaderFactory;
import javax.json.JsonString;
import javax.json.JsonStructure;
import javax.json.JsonValue;
import javax.json.JsonWriter;
import javax.json.JsonWriterFactory;
import javax.json.stream.JsonGenerator;
import javax.json.stream.JsonGeneratorFactory;
import javax.json.stream.JsonParser;
import javax.json.stream.JsonParserFactory;
public abstract class JsonProvider {
private static final String DEFAULT_PROVIDER = "org.glassfish.json.JsonProviderImpl";
protected JsonProvider() {
}
public static JsonProvider provider() {
ServiceLoader<JsonProvider> loader = ServiceLoader.load(JsonProvider.class);
Iterator<JsonProvider> it = loader.iterator();
if (it.hasNext()) {
return (JsonProvider)it.next();
} else {
try {
Class<?> clazz = Class.forName("org.glassfish.json.JsonProviderImpl");
return (JsonProvider)clazz.newInstance();
} catch (ClassNotFoundException var3) {
throw new JsonException("Provider org.glassfish.json.JsonProviderImpl not found", var3);
} catch (Exception var4) {
throw new JsonException("Provider org.glassfish.json.JsonProviderImpl could not be instantiated: " + var4, var4);
}
}
}
public abstract JsonParser createParser(Reader var1);
public abstract JsonParser createParser(InputStream var1);
public abstract JsonParserFactory createParserFactory(Map<String, ?> var1);
public abstract JsonGenerator createGenerator(Writer var1);
public abstract JsonGenerator createGenerator(OutputStream var1);
public abstract JsonGeneratorFactory createGeneratorFactory(Map<String, ?> var1);
public abstract JsonReader createReader(Reader var1);
public abstract JsonReader createReader(InputStream var1);
public abstract JsonWriter createWriter(Writer var1);
public abstract JsonWriter createWriter(OutputStream var1);
public abstract JsonWriterFactory createWriterFactory(Map<String, ?> var1);
public abstract JsonReaderFactory createReaderFactory(Map<String, ?> var1);
public abstract JsonObjectBuilder createObjectBuilder();
public JsonObjectBuilder createObjectBuilder(JsonObject object
<details>
<summary>英文:</summary>
I am trying to integrate the Hyperledger fabric SDK into the Spring boot, while I run the application I am facing the following error,
java.lang.UnsupportedOperationException: null
at javax.json.spi.JsonProvider.createObjectBuilder(JsonProvider.java:281) ~[javax.json-1.1.4.jar:1.1.4]
at javax.json.Json.createObjectBuilder(Json.java:327) ~[javax.json-1.1.4.jar:1.1.4]
at org.hyperledger.fabric.sdk.NetworkConfig.fromYamlStream(NetworkConfig.java:342) ~[fabric-sdk-java-1.4.8.jar:na]
at org.hyperledger.fabric.gateway.impl.GatewayImpl$Builder.networkConfig(GatewayImpl.java:99) ~[fabric-gateway-java-1.4.3.jar:na]
at org.hyperledger.fabric.gateway.impl.GatewayImpl$Builder.networkConfig(GatewayImpl.java:88) ~[fabric-gateway-java-1.4.3.jar:na]
at org.hyperledger.fabric.gateway.impl.GatewayImpl$Builder.networkConfig(GatewayImpl.java:69) ~[fabric-gateway-java-1.4.3.jar:na]
at com.person.fabricclient.controllers.PersonController.getPerson(PersonController.java:69) ~[classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_242]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_242]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_242]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_242]
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190) ~[spring-web-5.2.5.RELEASE.jar:5.2.5.RELEASE]
Following is the rest controller code,
@RequestMapping(value = "/hello/createperson",method = RequestMethod.GET)
public String getPerson() throws IOException {
Gateway.Builder builder = Gateway.createBuilder();
try {
// A wallet stores a collection of identities
Path walletPath = Paths.get("/home/Fabric-samples_1.4/fabric-samples/commercial-paper/organization/magnetocorp/identity/user/isabella/wallet");
Wallet wallet = Wallet.createFileSystemWallet(walletPath);
String userName = "User1@org1.example.com";
Path connectionProfile = Paths.get("/home/Fabric-samples_1.4/fabric-samples/commercial-paper/organization/magnetocorp/gateway/networkConnection.yaml");
// Set connection options on the gateway builder
builder.identity(wallet, userName).networkConfig(connectionProfile).discovery(false);
// Connect to gateway using application specified parameters
try(Gateway gateway = builder.connect()) {
// Access PaperNet network
System.out.println("Use network channel: mychannel.");
Network network = gateway.getNetwork("mychannel");
// Get addressability to commercial paper contract
System.out.println("Use org.papernet.commercialpaper smart contract.");
Contract contract = network.getContract("person", "org.go.smartcontract");
// Issue commercial paper
System.out.println("Submit commercial paper issue transaction.");
byte[] response = contract.submitTransaction("createCar", "go", "Hello", "2020-05-31");
// Process response
System.out.println("Process issue transaction response.");
// CommercialPaper paper = CommercialPaper.deserialize(response);
// System.out.println(paper);
} catch (TimeoutException e) {
e.printStackTrace();
} catch (ContractException e) {
e.printStackTrace();
}
} catch (InterruptedException | IOException e) {
e.printStackTrace();
System.exit(-1);
}
return "created";
}
When I tried to debug the issue is occurring in the following class, the service loader returns an object when I run in the spring boot, but when I try without spring boot, service loader does not return any object and this line gets executed
Class<?> clazz = Class.forName("org.glassfish.json.JsonProviderImpl");
in the following class.
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package javax.json.spi;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.ServiceLoader;
import javax.json.JsonArray;
import javax.json.JsonArrayBuilder;
import javax.json.JsonBuilderFactory;
import javax.json.JsonException;
import javax.json.JsonMergePatch;
import javax.json.JsonNumber;
import javax.json.JsonObject;
import javax.json.JsonObjectBuilder;
import javax.json.JsonPatch;
import javax.json.JsonPatchBuilder;
import javax.json.JsonPointer;
import javax.json.JsonReader;
import javax.json.JsonReaderFactory;
import javax.json.JsonString;
import javax.json.JsonStructure;
import javax.json.JsonValue;
import javax.json.JsonWriter;
import javax.json.JsonWriterFactory;
import javax.json.stream.JsonGenerator;
import javax.json.stream.JsonGeneratorFactory;
import javax.json.stream.JsonParser;
import javax.json.stream.JsonParserFactory;
public abstract class JsonProvider {
private static final String DEFAULT_PROVIDER = "org.glassfish.json.JsonProviderImpl";
protected JsonProvider() {
}
public static JsonProvider provider() {
ServiceLoader<JsonProvider> loader = ServiceLoader.load(JsonProvider.class);
Iterator<JsonProvider> it = loader.iterator();
if (it.hasNext()) {
return (JsonProvider)it.next();
} else {
try {
Class<?> clazz = Class.forName("org.glassfish.json.JsonProviderImpl");
return (JsonProvider)clazz.newInstance();
} catch (ClassNotFoundException var3) {
throw new JsonException("Provider org.glassfish.json.JsonProviderImpl not found", var3);
} catch (Exception var4) {
throw new JsonException("Provider org.glassfish.json.JsonProviderImpl could not be instantiated: " + var4, var4);
}
}
}
public abstract JsonParser createParser(Reader var1);
public abstract JsonParser createParser(InputStream var1);
public abstract JsonParserFactory createParserFactory(Map<String, ?> var1);
public abstract JsonGenerator createGenerator(Writer var1);
public abstract JsonGenerator createGenerator(OutputStream var1);
public abstract JsonGeneratorFactory createGeneratorFactory(Map<String, ?> var1);
public abstract JsonReader createReader(Reader var1);
public abstract JsonReader createReader(InputStream var1);
public abstract JsonWriter createWriter(Writer var1);
public abstract JsonWriter createWriter(OutputStream var1);
public abstract JsonWriterFactory createWriterFactory(Map<String, ?> var1);
public abstract JsonReaderFactory createReaderFactory(Map<String, ?> var1);
public abstract JsonObjectBuilder createObjectBuilder();
public JsonObjectBuilder createObjectBuilder(JsonObject object) {
throw new UnsupportedOperationException();
}
public JsonObjectBuilder createObjectBuilder(Map<String, Object> map) {
throw new UnsupportedOperationException();
}
public abstract JsonArrayBuilder createArrayBuilder();
public JsonArrayBuilder createArrayBuilder(JsonArray array) {
throw new UnsupportedOperationException();
}
public JsonPointer createPointer(String jsonPointer) {
throw new UnsupportedOperationException();
}
public JsonPatchBuilder createPatchBuilder() {
throw new UnsupportedOperationException();
}
public JsonPatchBuilder createPatchBuilder(JsonArray array) {
throw new UnsupportedOperationException();
}
public JsonPatch createPatch(JsonArray array) {
throw new UnsupportedOperationException();
}
public JsonPatch createDiff(JsonStructure source, JsonStructure target) {
throw new UnsupportedOperationException();
}
public JsonMergePatch createMergePatch(JsonValue patch) {
throw new UnsupportedOperationException();
}
public JsonMergePatch createMergeDiff(JsonValue source, JsonValue target) {
throw new UnsupportedOperationException();
}
public JsonArrayBuilder createArrayBuilder(Collection<?> collection) {
throw new UnsupportedOperationException();
}
public abstract JsonBuilderFactory createBuilderFactory(Map<String, ?> var1);
public JsonString createValue(String value) {
throw new UnsupportedOperationException();
}
public JsonNumber createValue(int value) {
throw new UnsupportedOperationException();
}
public JsonNumber createValue(long value) {
throw new UnsupportedOperationException();
}
public JsonNumber createValue(double value) {
throw new UnsupportedOperationException();
}
public JsonNumber createValue(BigDecimal value) {
throw new UnsupportedOperationException();
}
public JsonNumber createValue(BigInteger value) {
throw new UnsupportedOperationException();
}
}
</details>
# 答案1
**得分**: 3
问题出在服务加载器上,我必须在 META_INF/Services/\<与类名相似的文件名> 中指定应加载的正确类。文件应包含应加载的类。
<details>
<summary>英文:</summary>
The issues were with the service loader, I had to mention the proper class it should load in the META_INF/Services/<file name similar to class> the file should contain the class that should be loaded.
</details>
# 答案2
**得分**: 0
删除pom中的genson,并将其替换为com.fasterxml.jackson。
<details>
<summary>英文:</summary>
What helped me was to remove genson from my pom and replace it with com.fasterxml.jackson
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论