I am trying to build a rest api that should consume json/x-application data. Now I have looked into two libraries javax.json-api and org.json for handling the data.
Example JSON:
{
"error": "false",
"error_msg": "",
"version": "1.13.10",
"result": {
"malware": {
"finding1": {
"file": "/path/to/filep",
"malware": "{HEX}r2h.malware.blue.44"
}
}
},
"newest_version": "1.13.10"
}
If I now consume this with javax JsonObject, it will work and I can go on with my code. BUT, if i instead post this data and I use org.json.JSONObject I will receive response at the client:
Unrecognized field "error" (class org.json.JSONObject), not marked as ignorable
Tried to find responses on the web, but I didnt step over anything that explains this?
Regards and Thanks
Well,
I do not really know if there is a solution for this. Eventually the REST architectural style does not support JSONObject (org.json.JSONObject). However, the workaround is pretty easy, just consume the json as a String (still you can declare HTTP Request to enforce the type application/json).
So this could look like the following:
#Path("/myendpoint")
#POST
#Consumes(MediaType.APPLICATION_JSON)
public String receiveRequest(String json) {
JSONObject jo = new JSONObject(json);
...
}
First off all your json is valid json. Second thing you are getting a error in response, it means error is not in your code. And third thing the error is "Unrecognized field" it means the POJO class inside the client code does not contain field "error".
Related
I am integrating my app with a third party app and there is a requirement where I have to call their APIs. So, there is this GET call which when supplied with proper headers and params and subsequently called, returns some JSON data. Now obviously I have tried it in postman and it's working without any issues. But when I am making the same call in Java using Spring's RestTemplate (with exchange method), the JSON response I am getting is incomplete. Basically, it's giving me response like the part which is missing was never there in response. For example,
In Postman, the response looks like this:
{
"key1": object 1,
"key2": object 2
}
But in Java, the response looks like this:
{
"key1": object1
}
The response is incomplete. Also, after doing some analysis I have found that there is this response header: content-length, it's value in Postman is 933 and in Java it's 840. What can be done to solve this problem?
I was meet a problem when I tried used net/jsonrpc package to build a server and a Java client with jsonrpc4j
The problem is jsonrpc4j is when error happen, golang`s method will return error and encoding to json.
I got this json object in client
{"id": -6028374044949000, "result": null, "error": "some error return message"}
This object cast failed in java's json4j.
http://www.jsonrpc.org/specification#error_object
After I checked the jsonrpc page, it is said the error field MUST a json object with fields [code, message, date], the golang jsonrpc package not meet the require.
So I`m confused how to solve this.
Change the jsonrpc lib,
Just replace the rpc way to thrift/gRpc,
Avoid to return error but send error in reply and let Java check the response,
Or just edited the json4j or golang's source code ( I'm very horrible about this option)
Thanks for watch.
If you need JSON-RPC 2.0 support for Go you can try https://github.com/powerman/rpc-codec
I am getting different type of JSON response out of HTTP request API. There are might be couple of JSON format option coming back from API. For example it might be valid response with expected data but in some cases it might be internal server error detailed message.
At the moment I am using Gson to convert incoming string into the object, but since sometimes it comes as different format Gson not able to convert it as different template class is provided.
NOTE:
Error does not mean an exception. For example JSON body contain just information that authentication is failed for example, but call was made successfully and JSON body is VALID. HTTP is actually always successful and will be 200. Problem is that sometimes authentication might fail and it will return different JSON.
String response = restTemplate.getForObject(request, String.class);
ObjectResponse objResponse = gson.fromJson(response, ObjectResponse.class);
Could you please suggest better way of doing it so that I can handle different types of responses? Or maybe you know completely different way of doing it.
Thanks!
If you can't predict the structure of the response, map it to a tree of simple Java maps, arrays, and strings. The Jackson library supports this with 'readTree' methods. Once you look at the tree and decide what it is, you can then ask the library to map a tree to an object of a class.
One option is to make a class representing the JSON data, and deserialize into that. This way, if the data does not match that structure, you will get an exception.
When you try and create your object and it fails, catch the exception and try and decode it as an error - you can then deal with that case as you wish (and the potential case where it is neither the object you expect or a valid error).
Check HTTP Response Codes. If you receive a status code that isn't OK(200) then you shouldn't try to parse for a successful response. For instance you may check the code and handle response like this (the object types are not actual Java types, but are given to provide an example):
MyHttpResponse response = MyHttpHelper.execute(...);
int status = response.getMyStatusCode();
String responseData = response.getStringBody();
switch(status) {
case 200: {
//request is successful, parse valid data
break;
}
default: {
//request is not valid, parse error data
break;
}
}
Folks i am getting "parsererror" exception while making ajax request to server.On my dev box
its working fine but at pruction getting this issue for some users(not reproducible at local box)
I did google and found one of the probable cause can be data format mismatch . For example server is
sending jsonp format and client expecting json format or vice versa.But this is not the case with me
becoz its json at both cient and server side.
From serverside struts 2 converts java object(which contains list of map ) in to json string internally.so
I see no scope of error here.
So i suspect there is something in data .for example :=data like scott" but i coud not reproduce issue
even with this kind of data .Can you guys help me to identify what kind of data inside
json can cause this issue/ or it can be a different issue altogether?
$.ajax({
"url": myURL,
"success": function (json) {
},
"dataType": "json",
"cache": false,
"error": function (xhr, error, thrown) {
if (error == "parsererror") {
alert("Getting Error");
}
}
});
Is it possible to let Play Framework 1 handle a sent JSON in a post request? The developer connecting to the backend would not like to send in key/value pairs with an ampersand as separator, he wants to send in a JSON. Problem is that the data is null, i.e name is null. Is this possible to achieve?
What I have so far:
Controller
public static void myMethod(String name) {
Logger.info(name);
}
Routes file
POST /test Application.myMethod
And I send the request with header Content-Type: application/json and the data in the body {
"name": "A name"
}
I did not find any auto binding so what I did was this:
MyModel myModel = new GsonBuilder().create().fromJson(new InputStreamReader(request.body), MyModel.class);
And it worked perfectly!
Actually, I don't know how this worked with you, because it is to give the error: "Stream Closed". A workarround to this would be:
String JSON = request.params.data.get("body")[0];
MyModel myModel = new Gson().fromJson(JSON, MyModel.class);