简体   繁体   中英

RestClientException client shows empty response body for Spring Client authentication

I am using Spring Oauth client setup on my frontend. I'm authentication against my API, which returns this

 {
   error: "unauthorized"
   error_description: "User does not exist"
 }

using my rest client (Google Plug: Advance RestClient); which is expected.When I use Spring Oauth Client setup, I was expecting that the error object RestClientException would have that JSON result in the response body however it is empty. See attached image (Watch Console shows what's in the response body).

在此处输入图片说明

Right click the image and select open image in new tab to make the image bigger

Request sent

grant_type=c_password&username=test&password=test&client_id=test&client_secret=test

Response

 Date: Sun, 26 Apr 2015 20:59:45 GMT 
 Connection: close
 Cache-Control: no-store 
 Pragma: no-cache 
 Www-Authenticate: Bearer realm="api/", error="unauthorized",  error_description="User does not exist"
 Content-Type: application/json;charset=UTF-8 
 Server: Jetty(7.x.y-SNAPSHOT) 
 Via: 1.1 vegur 
 Raw
 JSON

This may be a RestTemplate bug - your server (or the proxy) is using a combination of Connection: close and no Content-Length headers.

One way to confirm this: make your server write Content-Length headers (in Spring, adding a ShallowEtagHeaderFilter will do that).

If this workaround fixes this, then this bug has been fixed in SPR-8016 - upgrading the client application to Spring 4.1.5 will solve this.

it seems the actual exception that you are getting is not RestClientException. Just debug the error, it would be something HttpClientErrorException. So catch this exception instead of above one. This exception has methods to get the desired result.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM