Comments (3)
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.
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.
The README.md explains everything very well. You must have some knowledge of jsonrpc to begin with though.
from jsonrpc4j.
Related Issues (20)
- Method parameter conversion error handling logic should preserve JSON-RPC id from the request HOT 2
- Cross language conversion of JsonRpcService
- 方法重载不支持
- 对于数值相关类型有bug,基于1.6版本
- Basic HTTP Client
- ResponseBodyAdvice not triggered
- Remove net.iharder:base64
- RemoteExporter in SpringBoot 3.0.0 HOT 4
- is jsonrpc4j dead or still supported / maintained ? HOT 2
- Deprecate `javax.portlet:portlet-api` and portlet support
- Add support for newer `jakarta.servlet.http` API into `JsonRpcServer`
- CALL FOR HELP
- Looking for someone to handle maintenance and releases of this library HOT 2
- And new annotation supports creating specially JsonRpcClient.
- AutoJsonRpcServiceImplExporter not set parallelBatchProcessingTimeout to JsonServiceExporter HOT 1
- Have any plans to release the latest version? HOT 3
- per request headers
- BaseUrl truncated when create JsonRpc bean
- ObjectMapper passed into JsonRpcMultiServer is ignored
- How to add swagger or open api to the project? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jsonrpc4j.