Jamie White
Jamie White

Reputation: 1620

java.util.LinkedHashMap cannot be cast to java.lang.String (RestTemplate)

Getting a Casting Exception on the Object returned from the API Call. I am using Spring's RestTemplate to make the API Calls. I know the reason why but unsure about the best solution. The error is on the following line:

The following fails with Exception:

Map map = restTemplate.postForObject(buildUri(yqlQuery),request, LinkedHashMap.class);

And this too:

Profile profile = restTemplate.postForObject(buildUri(yqlQuery),request, Profile.class);;

Exception

java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to java.lang.String
  org.springframework.social.yahoo.api.impl.YahooErrorHandler.handleClientErrors(YahooErrorHandler.java:50)
  org.springframework.social.yahoo.api.impl.YahooErrorHandler.handleError(YahooErrorHandler.java:33)
  org.springframework.web.client.RestTemplate.handleResponseError(RestTemplate.java:615)
  org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:573)
  org.springframework.web.client.RestTemplate.execute(RestTemplate.java:544)
  org.springframework.web.client.RestTemplate.postForObject(RestTemplate.java:347)
  org.springframework.social.yahoo.api.impl.ProfileTemplate.profile(ProfileTemplate.java:35)
  org.springframework.social.yahoo.connect.YahooAdapter.setConnectionValues(YahooAdapter.java:30)
  org.springframework.social.yahoo.connect.YahooAdapter.setConnectionValues(YahooAdapter.java:15)
  org.springframework.social.connect.support.AbstractConnection.setValues(AbstractConnection.java:174)
  org.springframework.social.connect.support.AbstractConnection.initKey(AbstractConnection.java:137)
  org.springframework.social.connect.support.OAuth1Connection.<init>(OAuth1Connection.java:59)
  org.springframework.social.connect.support.OAuth1ConnectionFactory.createConnection(OAuth1ConnectionFactory.java:59)
  org.springframework.social.security.provider.OAuth1AuthenticationService.getAuthToken(OAuth1AuthenticationService.java:112)
  org.springframework.social.security.SocialAuthenticationFilter.attemptAuthService(SocialAuthenticationFilter.java:239)
  org.springframework.social.security.SocialAuthenticationFilter.attemptAuthentication(SocialAuthenticationFilter.java:157)
  org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:211)
  org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
  org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:110)
  org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
  org.springframework.security.web.csrf.CsrfFilter.doFilterInternal(CsrfFilter.java:85)
  org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
  org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
  org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:57)
  org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
  org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
  org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87)
  org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
  org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:50)
  org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
  org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
  org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192)
  org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160)
  org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:344)
  org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:261)

Class Excerpt

public class ProfileTemplate extends AbstractOperations implements ProOps {
        private final RestTemplate restTemplate;
        @Override
        public Profile profile() throws JsonParseException, JsonMappingException, IOException,UnsupportedEncodingException {
            MultiValueMap<String, String> request = new LinkedMultiValueMap<String, String>();
            request.set("format","json");
            String yqlQuery = UriUtils.encodePath("...", "UTF-8");
            Map map = restTemplate.postForObject(buildUri(yqlQuery),request, LinkedHashMap.class);
            Profile profile = objectMapper().convertValue(map.get("profile"), Profile.class);
            return profile;
        }

}

Upvotes: 3

Views: 27998

Answers (1)

Jamie White
Jamie White

Reputation: 1620

The problem had to do with casting on YahooErrorHandler.java:50

Should be using toString() instead of direct cast on following line:

errorText = (String) errorMap.get("error");

Correct:

errorText = errorMap.get("error").toString();

Upvotes: 2

Related Questions