4

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!

necromancer
  • 23,916
  • 22
  • 68
  • 115
  • 1
    just letting you know that you can manually award the bounty http://stackoverflow.com/faq#bounty incase you are already satisfied with the accepted answer. helps to clear this question from the featured(questions with bounties) tab. – bool.dev Apr 07 '12 at 20:57

2 Answers2

7

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;
    }
}
shashankaholic
  • 4,122
  • 3
  • 25
  • 28
1

Try to change your ME class to following:

@XmlElement
public String id;
@XmlElement
public String name;

And add proper getters and setters for them(!). In addition I'd suggest you to move main() method to other class.

Alex Stybaev
  • 4,623
  • 3
  • 30
  • 44
  • Hi, Thank you for your comments, but it did not work. The problem is that I am trying to consume JSON (not XML). I have included the jersey-json dependency/jars but I am not properly configuring it to scan my Me.java class and generate an appropriate message body reader (I think). – necromancer Mar 29 '12 at 17:46
  • Adding the following lines works but it is ugly because I am unmarshalling it once as String from web_resource and then unmarshalling it again via a separate JSON context. I am sure there is a better way, and I hope somebody can point me to it. ` JSONJAXBContext json_jaxb_context = new JSONJAXBContext(Me.class); String response = web_resource.get(String.class); Me me = json_jaxb_context.createJSONUnmarshaller().unmarshalFromJSON(new StringReader(response), Me.class);` – necromancer Mar 29 '12 at 17:48