当前位置: 首页 > 知识库问答 >
问题:

java.util.LinkedHashMap不能强制转换为java.lang.String(RestTemplate)

赫连明诚
2023-03-14
Map map = restTemplate.postForObject(buildUri(yqlQuery),request, LinkedHashMap.class);
Profile profile = restTemplate.postForObject(buildUri(yqlQuery),request, Profile.class);;
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)
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;
        }

}

共有1个答案

殳越
2023-03-14

这个问题与YahooErrorHandler.java:50上的转换有关

应该使用toString()而不是直接强制转换以下行:

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

正确:

errorText = errorMap.get("error").toString();
 类似资料: