I am new to REST , JSON and Jersey usage. Now we got a requirement where we need to implement a Client which need need to query the server with JSON request, GET and going to receive a JSON String/Object, which need to be parsed and converted into local datastructure.
Could someone help me for the below points?
What I need to download for implementing REST Client application for supporting JSON ( we have restrictions to use Jersey apis, and no other third-party apis)
Sample java client code for requesting and parsing the JSON data.
"What do I need ?", It really depends of the technology you want to use with it .. eg, if you use glassfish (netbeans server), you can use their jersey "org.glassfish.jersey.jackson.JacksonFeature" as json provider and the other ressources glassfish includes. You won't need anything else for a simple implementation.
As you are a beginner, you better follow a tutorial like this one : http://java.dzone.com/news/simple-restful-web-services
If you want to convert String to a JSONObject -
Use this library - http://www.json.org/java/
JAR file is available at http://code.google.com/p/org-json-java/downloads/list
Use below code to convert a string to JSONObject -
JSONObject final_result = new JSONObject(result);
Related
I use Java (with Quarkus) for the backend.
Vue3 for the frontend.
API and Websockets to transition data between the two.
I am building a chat message application.
So far I was able to send a message inside the backend under a JSON format, do stuff in Java with this message (which holds the message inside a MessageJson class that I have created, hence the data type is now type of MessageJson), transform it into a String, then and send it back to the frontend. I wish to transform the String in the frontend, into a Json.
Here is my function which send the message back to the front (using Websockets):
public MessageResponseForm processBackend(MessageJson messageJson) {
MessageResponseForm messageResponseForm = new MessageResponseForm();
messages(messageJson);
webSocketConfig.sendMessage("U001", messageJson.toString());
return messageResponseForm;
}
The frontend retrieve it without issue, in the String format.
But I need it back into Json format.
I use the function JSON.parse() in Vue.js but it gives me this:
Which is normal in my opinion, because I should receive something like this from the backend:
"{"toRecipient":"U002","fromSender":"U00555","messageContent":"ewww555"}" .....
But instead I got this:
"MessageJson(toRecipient=U002, fromSender=U00555, messageContent=ewww555, dateCreated=2022-9-23 14:15:57, companyName=test)"
My Class MessageJson does not seems to keep the correct Json format.
How to prevent this?
Do I need to use the #Produces annotations in Quarkus? If yes, where exactly?
I found no other method related to ToString()...
EDIT: Do I need to use the dependency GSON to do the job, or can I do this natively?
I Want to Return a JSON response from server to client in gRPC.
one possible way is to convert it to string return the response then convert back to Json Object in client side, but i want to know can we do better?.
i am doing some google and found we can do it with the help of google.protobuf.struct
but didn't actually find any good example.
i want an example how i can use it as JSON in java.
If you are using proto3, one option is to define a protobuf message that mirrors the JSON object you wish to populate. Then you can use JsonFormat to convert between protobuf and JSON.
Using a com.google.protobuf.Struct instead of a self-defined message can also work. There is an example shown in the similar question.
Is there any api or something to convert RSS feed to Json?
I used the api rss2json api with restTemplate, it's working fine when you map it with an entity but this one doesn't support multiple requests as it's gets overloaded plus there is no documentation for it or support so if the api goes down so is my app and I couldn't find something similar besides the rome plugin that converts to an object. I want direct conversion to Json.
I'm not sure what you mean by direct conversion to JSON, but you could simply use org.json to convert XML String to JSON String:
String xml = ..
JSONObject jObject = XML.toJSONObject(xml);
String json = jObject.toString();
More discussion here: Converting xml to json using jackson
I finished by using Rome plugin and built my own JSon structure step by step. no better solution so far.
I have a couchbase database that is shared between multiple applications, storing documents as json. I cannot seem to get data into my java app, since it appears to be dependent on native java binary serialization.
This code:
CouchbaseClient client = new CouchbaseClient(hosts,"bucket","");
System.out.println((String)client.get("someKey"));
results in
net.spy.memcached.transcoders.SerializingTranscoder: Failed to decompress data
java.util.zip.ZipException: Not in GZIP format
since it is trying to deserialize by default. I notice that I can provide my own transcoder, but I really only want the raw string data so I can json parse it myself using gson or whatever. None of the available transcoders seem to give me this.
The couchbase docs have an example for setting json, but none for reading it. How are people reading json into java?
First off, this problem will go away soon in that the Couchbase "2.0 SDKs" implement common flags between each other so this kind of problem doesn't come up. Michael's blogs are a good read if you want to see what's happening here. The reason for the problem in the first place is that in the 1.x series, Couchbase was trying to stay compatible with existing application code and memcached. In the memcached world, the clients were all written by different people at different times.
Based on the exception, I believe you're probably trying to read an item stored by .NET. I have a sample transcoder you can use for this from a few weeks ago.
Make sure you are using latest CB java client:
<dependencies>
<dependency>
<groupId>com.couchbase.client</groupId>
<artifactId>couchbase-client</artifactId>
<version>1.4.4</version>
</dependency>
</dependencies>
see: Couchbase Java Client Library 1.4
I have my service that uses CB client running just fine. Here is how I create client:
CouchbaseConnectionFactoryBuilder cfb = new CouchbaseConnectionFactoryBuilder();
cfb.setOpTimeout(10000);
cfb.setOpQueueMaxBlockTime(5000);
CouchbaseClient client = new CouchbaseClient(cfb.buildCouchbaseConnection(baseURIs, bucketName, ""));
And here is an example how I get a raw string and convert it to POJOs:
MyPOJO get(CouchbaseClient client, String key)
{
com.google.gson.Gson gson = new com.google.gson.Gson();
String jsonValue = (String) client.get(key);
return gson.fromJson(jsonValue, MyPOJO.class);
}
Also, update your question with the sample JSON doc that causing this issue. Perhaps it has something to do with the format of the document itself.
i have some JSON data from server and i want to make java beans out of it.
can anybody help?
"recent_data":{[
"data_number":<data NUMBER>,
"bill_number":"<BILLING NUMBER>"
"data_date":"<data DATE>",
"due_date":"<DUE DATE>",
"data_amount":{
"amount_exchanged":"<AMOUNT EXCHANGED>"
"amount_deducted":"<AMOUNT DEDUCTED>",
"amount":"<AMOUNT>"
},
"total_amount":"TOTAL AMOUNT>",
"total_discounts":"<TOTAL_ DISCOUNTS>",
"adjusted_amount":"<ADJUSTED AMOUNT>",
"data_type":"Normal",
"data_status":"Open"
]}
You can use JSONObject to parse your json string and to populate java beans if you want that (or you could work directly with JSONObject.
It is a bit verbose, but it's built in in android.
Try Gson, it works better than JSONObject library.