Java – parses JSON responses into objects
•
Java
Good evening,
import javax.ws.rs.core.MediaType; import com.sun.jersey.api.client.Client; import com.sun.jersey.api.client.ClientResponse; import com.sun.jersey.api.client.WebResource; public class TestJerseyClient { public static void main(String[] args) { try { Client client = Client.create(); WebResource webResource = client .resource("http://path/to/service"); ClientResponse response = webResource .accept(MediaType.APPLICATION_JSON) .get(ClientResponse.class); if (response.getStatus() != 200) throw new RuntimeException("Failed : HTTP error code : " + response.getStatus()); String output = response.getEntity(String.class); System.out.println("Output from Server .... \n"); System.out.println(output); } catch (Exception e) { e.printStackTrace(); } } }
But I don't want to do string manipulation to get the response part I'm interested in
I prefer more structured methods, such as getting objects rather than strings (JSON objects, map < K, V > objects...)
Try #1 – map < K, V >
Sep 15,2013 2:46:13 AM com.sun.jersey.api.client.ClientResponse getEntity SEVERE: A message body reader for Java class java.util.Map,and Java type java.util.Map<java.lang.String,java.lang.Object>,and MIME media type application/json; charset=UTF-8 was not found Sep 15,2013 2:46:13 AM com.sun.jersey.api.client.ClientResponse getEntity SEVERE: The registered message body readers compatible with the MIME media type are: application/json; charset=UTF-8 -> com.sun.jersey.json.impl.provider.entity.JSONJAXBElementProvider$App com.sun.jersey.json.impl.provider.entity.JSONRootElementProvider$App com.sun.jersey.json.impl.provider.entity.JSONListElementProvider$App */* -> com.sun.jersey.core.impl.provider.entity.FormProvider com.sun.jersey.core.impl.provider.entity.StringProvider com.sun.jersey.core.impl.provider.entity.ByteArrayProvider com.sun.jersey.core.impl.provider.entity.FileProvider com.sun.jersey.core.impl.provider.entity.InputStreamProvider com.sun.jersey.core.impl.provider.entity.DataSourceProvider com.sun.jersey.core.impl.provider.entity.XMLJAXBElementProvider$General com.sun.jersey.core.impl.provider.entity.ReaderProvider com.sun.jersey.core.impl.provider.entity.DocumentProvider com.sun.jersey.core.impl.provider.entity.sourceProvider$StreamSourceReader com.sun.jersey.core.impl.provider.entity.sourceProvider$SAXSourceReader com.sun.jersey.core.impl.provider.entity.sourceProvider$DOMSourceReader com.sun.jersey.json.impl.provider.entity.JSONJAXBElementProvider$General com.sun.jersey.core.impl.provider.entity.XMLRootElementProvider$General com.sun.jersey.core.impl.provider.entity.XMLListElementProvider$General com.sun.jersey.core.impl.provider.entity.XMLRootObjectProvider$General com.sun.jersey.core.impl.provider.entity.EntityHolderReader com.sun.jersey.json.impl.provider.entity.JSONRootElementProvider$General com.sun.jersey.json.impl.provider.entity.JSONListElementProvider$General com.sun.jersey.api.client.ClientHandlerException: A message body reader for Java class java.util.Map,and MIME media type application/json; charset=UTF-8 was not found at com.sun.jersey.api.client.ClientResponse.getEntity(ClientResponse.java:561) at com.sun.jersey.api.client.ClientResponse.getEntity(ClientResponse.java:535) at com.sun.jersey.api.client.WebResource.handle(WebResource.java:696) at com.sun.jersey.api.client.WebResource.access$300(WebResource.java:74) at com.sun.jersey.api.client.WebResource$Builder.get(WebResource.java:512) at rest.TestJerseyClientAdvanced.main(TestJerseyClientAdvanced.java:36)
Solution
I suggest you use Jackson to group JSON responses This can be done in the following two steps
Step 1 Create a java bean whose member / object name matches the expected response For example, myresponse class
Step 2 Java beans are used when reading entities from client responses
private static ClientConfig clientConfig = new DefaultClientConfig(); clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING,Boolean.TRUE); ClientResponse response = Client.create(clientConfig). resource(uri).accept(MediaType.APPLICATION_JSON). header("content-type",MediaType.APPLICATION_JSON). get(MY_RESPONSE.class); MyResponse output = response.getEntity(MyResponse.class);
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码