I'm trying to hit a POST request using org.springframework.web.client.RestOperations
, This will return me a bearer token.
{"access_token": "<token>","expires_in": 300}
I have created a POJO to map to this response.
TokenResponse.class
import com.fasterxml.jackson.annotation.JsonInclude;import com.fasterxml.jackson.annotation.JsonProperty;import com.fasterxml.jackson.annotation.JsonPropertyOrder;@JsonInclude(JsonInclude.Include.NON_NULL)@JsonPropertyOrder({"access_token","expires_in"})public class TokenResponse { @JsonProperty("access_token") private String accessToken; @JsonProperty("expires_in") private Integer expiresIn; @JsonProperty("access_token") public String getAccessToken() { return accessToken; } @JsonProperty("access_token") public void setAccessToken(String accessToken) { this.accessToken = accessToken; } @JsonProperty("expires_in") public Integer getExpiresIn() { return expiresIn; } @JsonProperty("expires_in") public void setExpiresIn(Integer expiresIn) { this.expiresIn = expiresIn; }}
When I try to hit this POST request with the below code, it's returning me an error - org.springframework.web.client.HttpClientErrorException$NotAcceptable: 406 Not Acceptable: [no body]
ResponseEntity<T> response = restOperations.exchange( uri, HttpMethod.POST, new HttpEntity<>(body, headers), TokenResponse.class );
I know the error says there's no body, but when I change the last parameter in the above method call to String.class
, I'm getting a Http 200 along with the correct response body. where am I going wrong?