从API调用返回的对象获取强制转换异常。我正在使用Spring的RestTemplate进行API调用。我知道原因,但不确定最佳解决方案。错误发生在以下行:
以下因异常而失败:
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;
}
}
答案 0 :(得分:3)
问题与在YahooErrorHandler.java:50
上进行投射有关应该在下一行使用toString()而不是直接强制转换:
errorText = (String) errorMap.get("error");
正确:
errorText = errorMap.get("error").toString();