I know there's some answers out there but none of them really solved my problem. I'm really glad for all the input I can get. Thx in advance.
The Service I try to consume is a jersey rs 1.8 which I try to 'GET'. The Service is annotated with '@Produces(MediaType.APPLICATION_JSON)' and returns the following:
[{"projRnd":"1","firstname":"Jadeveon","lastname":"Clowney"},{"projRnd":"1","firstname":"Greg","lastname":"Robinson"}, ..]
Now my Service Consumer does the following:
import org.json.simple.JSONArray;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
...
WebResource webResource = client.resource(URL to my ws);
JSONArray jsonObjects = webResource.accept(MediaType.APPLICATION_JSON_TYPE).get(JSONArray.class);
Tomcat says:
HTTP Status 500
javax.servlet.ServletException: com.sun.jersey.api.client.ClientHandlerException: A message body reader for Java class org.json.simple.JSONArray, and Java type class org.json.simple.JSONArray, and MIME media type application/json was not found
...
To clarify ... the client runs on a vaadin application in the frontend ... the HTTP 500 is not from the backend.
My pom.xml sure has the right dependency:
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.8</version>
</dependency>
Like I said ... all the Input is much appreciated.
.. so I solved the Problem... first of all you shouldn't do something like:
JSONArray jsonObjects = webResource.accept(MediaType.APPLICATION_JSON_TYPE).get(JSONArray.class);
I changed the service so the response looks like:
{"prospectList":[{"projRnd":"1","firstname":"Jadeveon","lastname":"Clowney"},{"projRnd":"1","firstname":"Greg","lastname":"Robinson"}, ..]}
The way to get the JSONArray from this is pretty straight forward with a JSONParser which I didn't even try to use in the first place (FAIL!).
ClientResponse response = webResource.accept("application/json")
.type("application/json").get(ClientResponse.class);
String s = response.getEntity(String.class);
JSONParser parser = new JSONParser();
Object obj = parser.parse(s);
JSONObject jsonObject = (JSONObject) obj;
JSONArray jsonArray = (JSONArray) jsonObject.get("prospectList");
the returning jsonArray is iterable and the attributes of each element can now be accessed:
(String) jsonObj.get("firstname");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.