0

I have a Jersey client that makes a call to a 3rd party rest api and retrieves some JSON.

{"A":1,"W":2,"List":[{"name":"John","amount":10.0}]}

After that I need to append this JSON to my response class and give it back in the response.

@XmlRootElement
public class MyResponse {

    private JsonObject body;
    private String status;

I manage to assign the value that comes from the 3rd party api to body but the response that's sent is like this:

{
"status": "success",
"body": {
"entry": [
  {
  "key": "A",
  "value": 1
  }  ,
  {
  "key": "W",
  "value": 2
  },
  {
  "key": "List",
  "value": "[{\"name\":\"John\",\"amount\":10.0}]"
  }
]
}
}

So there are two main issues, moxy is generating key and value elements while I would like it to be key: value and also it is not generating properly the 2nd level objects in the JSON structure provided by the API.

Dennis Kriechel
  • 3,719
  • 14
  • 40
  • 62
themonkey
  • 377
  • 1
  • 3
  • 9

1 Answers1

1

MOXy is a JAXB implementation, while JsonObject is part of JSON-P. MOXy happens to be able to deal with JSON too, but that is a proprietary extension over the JAXB standard. As far as I know, there is no default mapping available between JSON-P and JAXB. The reason you're seeing those key/value entries must be because JsonObject extends java.util.Map, so you get the default MOXy mapping for that type.

I think you have the following possibilities:

  1. Go with either JSON-P or JAXB/MOXy (MOXy required for its additional JSON binding) only.
  2. Use one of the JAXB/MOXy mechanisms for mapping custom types from/to JAXB. The standard way is to use an XmlAdapter, examples for dealing with Maps in particular are here and here. But I think this will be difficult if you don't know the structure of the 3rd party JSON content and want to keep nested levels intact.

Yet another possibility might be to use a proprietary API like Jackson, but I can't help with that.

Hein Blöd
  • 1,553
  • 1
  • 18
  • 25