Giter VIP home page Giter VIP logo

Comments (3)

crypto-sporidium avatar crypto-sporidium commented on September 7, 2024 1

I looked at a number of different options. I tried to use the standard java 7 libs where possible.
In the end I found it easier to write a simple invoke method and add that to my code rather than try to get something else working...

This is my call json RPC function using the javax.json.JsonStructure that comes with java 7 EE

import javax.json.Json;
import javax.json.JsonObject;
import javax.json.JsonObjectBuilder;
import javax.json.JsonReader;
import javax.json.JsonStructure;

public host URL = null // set this to the URL for the json service

public JsonStructure remoteCall(final JsonObject jsondata) throws IOException {

    byte[] data = jsondata.toString().getBytes();
    con = (HttpURLConnection) host.openConnection();
    con.setDoOutput(true);
    con.setUseCaches(true);
    con.setRequestMethod("POST");
    con.setRequestProperty("Content-Type", "application/json");
    con.setRequestProperty("Accept", "application/json");
    con.setRequestProperty("Content-Length", Integer.toString(data.length));
    try (OutputStream out = con.getOutputStream()) {
        out.write(data);
        out.flush();
    }
    if (con.getResponseCode() != HttpURLConnection.HTTP_OK) {
        // Fail
        JsonObjectBuilder error = Json.createObjectBuilder();
        error.addNull("result");
        error.add("error", con.getResponseMessage());
        error.add("id", jsondata.getJsonString("id"));
        return error.build();
        // OK
    } else {
        try (InputStream incomming = con.getInputStream()) {
            JsonStructure repply;
            try (JsonReader reader = Json.createReader(incomming)) {
                repply = reader.read();
                return repply;
            }
        }
    }
} 

It worked pretty well

To provide https

Just cast the con to an https connection
con = (HttpsURLConnection) host.openConnection();

to do authentication in your class constructor add something like this

public RemoteService(URL host, final String username, final String password) {

    user = username;
    pass = password;
    this.host = host;

    Authenticator.setDefault(new Authenticator() {
        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(user, pass.toCharArray());
        }
    });

}

from jsonrpc4j.

lordscales91 avatar lordscales91 commented on September 7, 2024

It uses The Jackson Library for JSON deserialization/serialization. But it is used internally and don't let the programmer change the options. On the other hand you CAN'T deserialize an Interface because you can't instantiate it, but if you provide a field indicating the implementation you can work do the correct deserialization externally, by providing a request listener which will receive the response in an Jackson's ObjectMapper, you can obtain the original JSON from that object and use Gson which is a better JSON library. With Gson you can create a JSON Deserializer implementation for the interface and create the correct implementation... Damn I'm at work right now and I'm loosing my time helping some random user... I hope this will be useful to you. I will tell you how I use this library, I have a class called RpcClient which is a wrapper of JsonHttpClient, I pass to it a reference to the original caller, the method, the params, the class type, and a symbolic string indicating the type of request (request for get a list of clients, get a single client, etc...). I call the invoke method of jsonhttpclient passing the appropiate parameters(which I received from the original caller), then I do a callback to the original caller passing the response as a generic object and the request type, then on the original caller I do the appropiate cast based on the request type and work with it. To handle Exceptions, I will pass the exception object of the catch to the original caller, so I can use instance of to check if the response it's an exception and show an error message to the user.

from jsonrpc4j.

briandilley avatar briandilley commented on September 7, 2024

The README.md explains everything very well. You must have some knowledge of jsonrpc to begin with though.

from jsonrpc4j.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.