0

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

Ilir
  • 430
  • 2
  • 7
  • 19

2 Answers2

1

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);
...
}
Elis Byberi
  • 1,422
  • 1
  • 11
  • 20
Ilir
  • 430
  • 2
  • 7
  • 19
0

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".

Anirudh Jadhav
  • 967
  • 1
  • 9
  • 23
  • But thats the thing, I have not mapped anything to any pojo yet. Thats what confuses me about this. – Ilir Apr 14 '18 at 21:09