My code is not working:
package com.foo.json;
import javax.xml.bind.annotation.XmlRootElement;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
@XmlRootElement
public class Me {
public String id;
public String name;
public static void main(String[] args) {
Client client = Client.create();
WebResource web_resource = client.resource("https://graph.facebook.com/me?fields=id,name&access_token=deleted");
Me response = web_resource.get(Me.class);
System.out.println(response);
}
}
The error is:
Mar 28, 2012 11:56:43 AM com.sun.jersey.api.client.ClientResponse getEntity
SEVERE: A message body reader for Java class com.foo.json.Me, and Java type class com.foo.json.Me, and MIME media type text/javascript; charset=UTF-8 was not found
Mar 28, 2012 11:56:43 AM com.sun.jersey.api.client.ClientResponse getEntity
SEVERE: The registered message body readers compatible with the MIME media type are:
*/* ->
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.json.impl.provider.entity.JSONArrayProvider$General
com.sun.jersey.json.impl.provider.entity.JSONObjectProvider$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.json.impl.provider.entity.JacksonProviderProxy
Exception in thread "main" com.sun.jersey.api.client.ClientHandlerException: A message body reader for Java class com.foo.json.Me, and Java type class com.foo.json.Me, and MIME media type text/javascript; charset=UTF-8 was not found
at com.sun.jersey.api.client.ClientResponse.getEntity(ClientResponse.java:550)
at com.sun.jersey.api.client.ClientResponse.getEntity(ClientResponse.java:506)
at com.sun.jersey.api.client.WebResource.handle(WebResource.java:684)
at com.sun.jersey.api.client.WebResource.get(WebResource.java:191)
at com.foo.json.Me.main(Me.java:18)
The response from curl-ing the URL is:
$ curl --head https://graph.facebook.com/me?fields=id,name\&access_token=deleted
HTTP/1.1 200 OK
Access-Control-Allow-Origin: *
Cache-Control: private, no-cache, no-store, must-revalidate
Content-Type: text/javascript; charset=UTF-8
Expires: Sat, 01 Jan 2000 00:00:00 GMT
Pragma: no-cache
X-FB-Rev: 532491
X-FB-Debug: EGBmGLM1xfMiWi8KILCkQaG6FTn+WnRufgVY7FXDxnQ=
X-Cnection: close
Content-Length: 0
Date: Wed, 28 Mar 2012 18:58:53 GMT
and the content is:
$curl --head https://graph.facebook.com/me?fields=id,name\&access_token=deleted
{"id":"100001234567890","name":"My Name"}
What am I doing wrong? I do want to use the Jersey Client library and would prefer not to have to switch to another library. Thanks!
You just have to mention Accept header of the response to application/json as it is coming as json. In absence of it, it is treated as text/javascript response(check headers of your curl response) which is giving you ClientHandlerException.
Running code:
TestClient.java
public class TestClient {
public static void main(String[] args) {
Client client = Client.create();
WebResource web_resource = client.resource("https://graph.facebook.com/me?fields=id,name&access_token=deleted");
MyBean response=web_resource.accept(MediaType.APPLICATION_JSON).get(MyBean.class);
System.out.println(response.getId());
}
}
MyBean.java
@XmlRootElement
public class MyBean{
String id;
String name;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With