在尝试将Hyperledger Fabric SDK与Spring Boot集成时,出现以下错误:

huangapple go评论86阅读模式
英文:

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 = &quot;/hello/createperson&quot;,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(&quot;/home/Fabric-samples_1.4/fabric-samples/commercial-paper/organization/magnetocorp/identity/user/isabella/wallet&quot;);
                Wallet wallet = Wallet.createFileSystemWallet(walletPath);
    
                String userName = &quot;User1@org1.example.com&quot;;
    
                Path connectionProfile = Paths.get(&quot;/home/Fabric-samples_1.4/fabric-samples/commercial-paper/organization/magnetocorp/gateway/networkConnection.yaml&quot;);
    
                // 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(&quot;Use network channel: mychannel.&quot;);
                    Network network = gateway.getNetwork(&quot;mychannel&quot;);
    
                    // Get addressability to commercial paper contract
                    System.out.println(&quot;Use org.papernet.commercialpaper smart contract.&quot;);
                    Contract contract = network.getContract(&quot;person&quot;, &quot;org.go.smartcontract&quot;);
    
                    // Issue commercial paper
                    System.out.println(&quot;Submit commercial paper issue transaction.&quot;);
                    byte[] response = contract.submitTransaction(&quot;createCar&quot;, &quot;go&quot;, &quot;Hello&quot;, &quot;2020-05-31&quot;);
    
                    // Process response
                    System.out.println(&quot;Process issue transaction response.&quot;);
    //                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 &quot;created&quot;;
        }


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&lt;?&gt; clazz = Class.forName(&quot;org.glassfish.json.JsonProviderImpl&quot;);
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 = &quot;org.glassfish.json.JsonProviderImpl&quot;;
    
        protected JsonProvider() {
        }
    
        public static JsonProvider provider() {
            ServiceLoader&lt;JsonProvider&gt; loader = ServiceLoader.load(JsonProvider.class);
            Iterator&lt;JsonProvider&gt; it = loader.iterator();
            if (it.hasNext()) {
                return (JsonProvider)it.next();
            } else {
                try {
                    Class&lt;?&gt; clazz = Class.forName(&quot;org.glassfish.json.JsonProviderImpl&quot;);
                    return (JsonProvider)clazz.newInstance();
                } catch (ClassNotFoundException var3) {
                    throw new JsonException(&quot;Provider org.glassfish.json.JsonProviderImpl not found&quot;, var3);
                } catch (Exception var4) {
                    throw new JsonException(&quot;Provider org.glassfish.json.JsonProviderImpl could not be instantiated: &quot; + var4, var4);
                }
            }
        }
    
        public abstract JsonParser createParser(Reader var1);
    
        public abstract JsonParser createParser(InputStream var1);
    
        public abstract JsonParserFactory createParserFactory(Map&lt;String, ?&gt; var1);
    
        public abstract JsonGenerator createGenerator(Writer var1);
    
        public abstract JsonGenerator createGenerator(OutputStream var1);
    
        public abstract JsonGeneratorFactory createGeneratorFactory(Map&lt;String, ?&gt; 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&lt;String, ?&gt; var1);
    
        public abstract JsonReaderFactory createReaderFactory(Map&lt;String, ?&gt; var1);
    
        public abstract JsonObjectBuilder createObjectBuilder();
    
        public JsonObjectBuilder createObjectBuilder(JsonObject object) {
            throw new UnsupportedOperationException();
        }
    
        public JsonObjectBuilder createObjectBuilder(Map&lt;String, Object&gt; 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&lt;?&gt; collection) {
            throw new UnsupportedOperationException();
        }
    
        public abstract JsonBuilderFactory createBuilderFactory(Map&lt;String, ?&gt; 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/&lt;file name similar to class&gt; 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>



huangapple
  • 本文由 发表于 2020年4月4日 21:39:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/61028961.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定