Announcement Announcement Module
No announcement yet.
exception when invoking methods on twitterTemplate Page Title Module
Move Remove Collapse
Conversation Detail Module
  • 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
    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(
    at org.springframework.web.client.RestTemplate.execut e(
    at org.springframework.web.client.RestTemplate.postFo rEntity(
    at .updateStatus(
    at xx.xxxxxxx.SpringSocialApp.TwitterApp.main(Twitter
    I am getting this when I do both:
    String profileId = twitterTemplate.getProfileId();
    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.


    It appears that the following converters are used:
    [org.springframework.http.converter.ByteArrayHttpMe [email protected], org.springframework.http.converter.StringHttpMessa [email protected], org.springframework.http.converter.ResourceHttpMes [email protected], org.springframework.http.converter.xml.SourceHttpM [email protected], org.springframework.http.converter.xml.XmlAwareFor [email protected], org.springframework.http.converter.xml.Jaxb2RootEl [email protected]]

    I found a piece of code in the source when the RestTemplate is created:
    	private static final boolean jaxb2Present =
    			ClassUtils.isPresent("javax.xml.bind.Binder", RestTemplate.class.getClassLoader());
    	private static final boolean jacksonPresent =
    			ClassUtils.isPresent("", 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, 09:51 AM. Reason: formating

  • #2
    Jackson needed


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


    • #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.


      • #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!



        • #5

          Adding the following jackson dependancy fixed my problem:
          			<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. -->