Announcement Announcement Module
Collapse
No announcement yet.
exception when invoking methods on twitterTemplate Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • exception when invoking methods on twitterTemplate

    Hi to all,

    I am developing an example for accessing Twitter. All goes well when I do the oAuth "dance". At the end when I try to invoke the available methods on the TwitterTemplate I get:
    Exception in thread "main" org.springframework.web.client.RestClientException : Could not extract response: no suitable HttpMessageConverter found for response type [java.util.Map] and content type [application/json;charset=utf-8]
    at org.springframework.web.client.HttpMessageConverte rExtractor.extractData(HttpMessageConverterExtract or.java:77)
    at org.springframework.web.client.RestTemplate$Respon seEntityResponseExtractor.extractData(RestTemplate .java:619)
    at org.springframework.web.client.RestTemplate$Respon seEntityResponseExtractor.extractData(RestTemplate .java:1)
    at org.springframework.web.client.RestTemplate.doExec ute(RestTemplate.java:446)
    at org.springframework.web.client.RestTemplate.execut e(RestTemplate.java:401)
    at org.springframework.web.client.RestTemplate.postFo rEntity(RestTemplate.java:302)
    at org.springframework.social.twitter.TwitterTemplate .updateStatus(TwitterTemplate.java:134)
    at xx.xxxxxxx.SpringSocialApp.TwitterApp.main(Twitter App.java:121)
    I am getting this when I do both:
    Code:
    String profileId = twitterTemplate.getProfileId();
    and
    Code:
    twitterTemplate.updateStatus("yupiiiiii!");
    I found this post where one of the users mentions doing a try-catch block and ignoring the exception. This would go well for the updateStatus method (since I can see that the status gets updated). But creates a problem when you need to get something from the twitter server.

    Any solution? None of you get this exception?!

    If you need more details I would be glad to provide them.

    Thanks,
    Despot

    SOME UPDATE:
    It appears that the following converters are used:
    [org.springframework.http.converter.ByteArrayHttpMe ssageConverter@9bc984, org.springframework.http.converter.StringHttpMessa geConverter@1cb2795, org.springframework.http.converter.ResourceHttpMes sageConverter@93bca2, org.springframework.http.converter.xml.SourceHttpM essageConverter@1455d1c, org.springframework.http.converter.xml.XmlAwareFor mHttpMessageConverter@e3ffdf, org.springframework.http.converter.xml.Jaxb2RootEl ementHttpMessageConverter@b3a5a0]

    I found a piece of code in the source when the RestTemplate is created:
    Code:
    	private static final boolean jaxb2Present =
    			ClassUtils.isPresent("javax.xml.bind.Binder", RestTemplate.class.getClassLoader());
    
    	private static final boolean jacksonPresent =
    			ClassUtils.isPresent("org.codehaus.jackson.map.ObjectMapper", RestTemplate.class.getClassLoader()) &&
    					ClassUtils.isPresent("org.codehaus.jackson.JsonGenerator", RestTemplate.class.getClassLoader());
    
    	private static boolean romePresent =
    			ClassUtils.isPresent("com.sun.syndication.feed.WireFeed", RestTemplate.class.getClassLoader());
            public RestTemplate() {
    		this.messageConverters.add(new ByteArrayHttpMessageConverter());
    		this.messageConverters.add(new StringHttpMessageConverter());
    		this.messageConverters.add(new ResourceHttpMessageConverter());
    		this.messageConverters.add(new SourceHttpMessageConverter());
    		this.messageConverters.add(new XmlAwareFormHttpMessageConverter());
    		if (jaxb2Present) {
    			this.messageConverters.add(new Jaxb2RootElementHttpMessageConverter());
    		}
    		if (jacksonPresent) {
    			this.messageConverters.add(new MappingJacksonHttpMessageConverter());
    		}
    		if (romePresent) {
    			this.messageConverters.add(new AtomFeedHttpMessageConverter());
    			this.messageConverters.add(new RssChannelHttpMessageConverter());
    		}
    	}
    A wild guess: since the exception is all about not being able to resolve application/json content type, and on the other hand I cannot see the Jackson converter in the available converters, does this mean I am missing a library? Is spring-social-core.jar enough or should I include some other libraries in the project?
    Last edited by despot; Jan 18th, 2011, 10:51 AM. Reason: formating

  • #2
    Jackson needed

    Hi,

    I got the same problem and managed to fix it by including the Jackson Library

    http://jackson.codehaus.org/

    Comment


    • #3
      Yes, Jackson is a required because the data coming back from Twitter is in JSON format.

      Under the covers of TwitterTemplate is RestTemplate, which uses implementations of HttpMessageConverter to convert data coming back from a REST service into some type that the caller asks for. There will only be a JSON message converter if Jackson is in the classpath. Otherwise, without a suitable JSON message converter registered, you'll get an exception saying that there's no suitable message converter to convert between application/json and whatever type RestTemplate is asked to give back.

      Comment


      • #4
        Thanks for your replies! I am unable to check this because of problems with the maven repository, but I am sure it will work. I will inform you about my progress!

        Regards,
        Despot

        Comment


        • #5
          Solution

          Adding the following jackson dependancy fixed my problem:
          Code:
                          <dependency>
          			<groupId>org.codehaus.jackson</groupId>
          			<artifactId>jackson-mapper-asl</artifactId>
          			<version>[1.0,2.0)</version> <!-- 1.0 <= x < 2.0 so all versions higher than 1.0 including 1.0 but lower 
          				than (and excluding) 2.0, since the version of jackson 2.0 might not be compatible. -->
          		</dependency>
          Regards,
          Despot

          Comment

          Working...
          X