gpt4 book ai didi

java - 找不到适合响应类型和内容类型的 HttpMessageConverter

转载 作者:行者123 更新时间:2023-12-05 07:42:44 26 4
gpt4 key购买 nike

我正在尝试获取 ResponseEntity with spring rest 模板,如下所示。我收到以下错误,

Caused by: org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [interface org.springframework.security.oauth2.common.OAuth2AccessToken] and content type [application/json;charset=ISO-8859-1]

代码:

ResourceOwnerPasswordResourceDetails resource = new ResourceOwnerPasswordResourceDetails();

resource.setAccessTokenUri("https://.../oauth2/token");
resource.setUsername("u");
resource.setPassword("p");
resource.setClientSecret(".....s");
resource.setClientId("....i");
resource.setGrantType("password");
List<String> scopes = new ArrayList<String>(2);
scopes.add("write");
scopes.add("read");
resource.setScope(scopes);
OAuth2RestTemplate oAuthRestTemplate = new OAuth2RestTemplate(resource);

try{
SSLUtil.turnOffSslChecking();
ResponseEntity<String> result = oAuthRestTemplate.getForEntity("https:......." + "/api/version", String.class);
System.out.println(result);
} catch (RestClientException e) {
e.printStackTrace();
}

我尝试将媒体类型设置为 Application/json,如 this post.但仍然是同样的错误。

完整跟踪:

Exception in thread "main" error="access_denied", error_description="Error requesting access token."
at org.springframework.security.oauth2.client.token.OAuth2AccessTokenSupport.retrieveToken(OAuth2AccessTokenSupport.java:121)
at org.springframework.security.oauth2.client.token.grant.password.ResourceOwnerPasswordAccessTokenProvider.obtainAccessToken(ResourceOwnerPasswordAccessTokenProvider.java:47)
at org.springframework.security.oauth2.client.token.AccessTokenProviderChain.obtainNewAccessTokenInternal(AccessTokenProviderChain.java:142)
at org.springframework.security.oauth2.client.token.AccessTokenProviderChain.obtainAccessToken(AccessTokenProviderChain.java:118)
at org.springframework.security.oauth2.client.OAuth2RestTemplate.acquireAccessToken(OAuth2RestTemplate.java:216)
at org.springframework.security.oauth2.client.OAuth2RestTemplate.getAccessToken(OAuth2RestTemplate.java:168)
at org.springframework.security.oauth2.client.OAuth2RestTemplate.createRequest(OAuth2RestTemplate.java:89)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:479)
at org.springframework.security.oauth2.client.OAuth2RestTemplate.doExecute(OAuth2RestTemplate.java:123)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:446)
at org.springframework.web.client.RestTemplate.getForEntity(RestTemplate.java:236)
at com.ClientTemplate.main(ClientTemplate.java:56)
Caused by: org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [interface org.springframework.security.oauth2.common.OAuth2AccessToken] and content type [application/json;charset=ISO-8859-1]
at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:107)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:491)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:454)
at org.springframework.security.oauth2.client.token.OAuth2AccessTokenSupport.retrieveToken(OAuth2AccessTokenSupport.java:113)
... 11 more

最佳答案

您需要添加 MappingJackson2HttpMessageConverter 并将内容类型添加到标题中 application/json 。参见 here

关于java - 找不到适合响应类型和内容类型的 HttpMessageConverter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44263935/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com