2017-06-30 38 views
0

我试图如图所示,发布和使用URL邮递员Chrome应用发布,没有合适的HttpMessageConverter发现错误,同时使用RestTemplate

URL = http://example.com/asg/oauth2/access_token?grant_type=client_credentials&scope=public&client_id=abc-xyz-123-456&client_secret=yhlk-pkli-asfd-hgjf

我得到一个JSON响应:

{ 
"expires_in": 170889, 
"token_type": "Bearer", 
"access_token": "JKL7ZQnRjPqjFVkEArOVpiJs" 
} 

现在我的春季启动应用程序版本1.5,我做了一个帖子,如下所示,这会引发错误。

@Autowired 
RestTemplate restTemplate 
private ApiAccessToken ApiAccessToken; 

ApiAccessToken = restTemplate.postForObject(url, null, ApiAccessToken.class); 

ApiAccessToken类:

public class ApiAccessToken { 
    private String expires_in; 
    private String token_type; 
    private String access_token; 
    // ommitted getters & setters 
    } 

的pom.xml

`<dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-web</artifactId> 
    </dependency>` 

错误:

org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [class com.example.ApiAccessToken] and content type [application/json] 
at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:110) ~[spring-web-4.3.9.RELEASE.jar:4.3.9.RELEASE] 
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:655) ~[spring-web-4.3.9.RELEASE.jar:4.3.9.RELEASE] 
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:613) ~[spring-web-4.3.9.RELEASE.jar:4.3.9.RELEASE] 
at org.springframework.web.client.RestTemplate.postForObject(RestTemplate.java:380) ~[spring-web-4.3.9.RELEASE.jar:4.3.9.RELEASE] 
at com.example.ApiService.setOrUpdateApiAccessToken(ApiService.java:61) ~[classes/:na] 
at com.example.Controller.home(Controller.java:16) ~[classes/:na] 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_11] 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_11] 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_11] 
at java.lang.reflect.Method.invoke(Method.java:483) ~[na:1.8.0_11] 
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205) ~[spring-web-4.3.9.RELEASE.jar:4.3.9.RELEASE] 
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133) ~[spring-web-4.3.9.RELEASE.jar:4.3.9.RELEASE] 
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:97) ~[spring-webmvc-4.3.9.RELEASE.jar:4.3.9.RELEASE] 
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827) ~[spring-webmvc-4.3.9.RELEASE.jar:4.3.9.RELEASE] 
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738) ~[spring-webmvc-4.3.9.RELEASE.jar:4.3.9.RELEASE] 
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) ~[spring-webmvc-4.3.9.RELEASE.jar:4.3.9.RELEASE] 
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967) ~[spring-webmvc-4.3.9.RELEASE.jar:4.3.9.RELEASE] 
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901) ~[spring-webmvc-4.3.9.RELEASE.jar:4.3.9.RELEASE] 
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970) ~[spring-webmvc-4.3.9.RELEASE.jar:4.3.9.RELEASE] 
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861) ~[spring-webmvc-4.3.9.RELEASE.jar:4.3.9.RELEASE] 
at javax.servlet.http.HttpServlet.service(HttpServlet.java:635) ~[tomcat-embed-core-8.5.15.jar:8.5.15] 
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846) ~[spring-webmvc-4.3.9.RELEASE.jar:4.3.9.RELEASE] 
at javax.servlet.http.HttpServlet.service(HttpServlet.java:742) ~[tomcat-embed-core-8.5.15.jar:8.5.15] 
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) ~[tomcat-embed-core-8.5.15.jar:8.5.15] 
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.15.jar:8.5.15] 
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) ~[tomcat-embed-websocket-8.5.15.jar:8.5.15] 
+0

你需要有杰克逊在运行时classath。 –

+0

我在我的类路径中添加了spring-boot-starter-web作为依赖项。这不会有帮助吗? – dazzle

+0

是的,它应该添加杰克逊作为依赖。 –

回答

0

似乎是一个配置问题。 请确保您有安装在您的Servlet过滤器链(servlet的context.xml中)一个JSON序列

This answer还可以帮助

相关问题