Parse a JSON response as an object(将 JSON 响应解析为对象)
问题描述
Good evening,
I am a newbie in web services and i just want to write a simple client that simply gets responses from REST calls.
I downloaded and added jersey-bundle-1.17.1.jar
to my build path and i found a working piece of code that makes a REST call to a url and returns a response as a String
.
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 ....
");
System.out.println(output);
} catch (Exception e) {
e.printStackTrace();
}
}
}
But i don't want to do string manipulations to get the parts of the response that im interested in.
I would prefer a more structured approach, like getting an Object
instead of a string
(a JSON
Object, a Map<K,V>
Object, etc...)
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 Java type java.util.Map<java.lang.String, java.lang.Object>, 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)
I recommend you use Jackson for (un)marshalling JSON responses. This can be done is two steps as below.
Step1. create a java bean with member/object names that match the expected response. e.g, MyResponse.class
Step2. use the java bean when reading the entity from the client response.
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);
这篇关于将 JSON 响应解析为对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:将 JSON 响应解析为对象
基础教程推荐
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- 降序排序:Java Map 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01