Reputation: 79
I am new to Spring MVC, I am using below code to get response from another application. 4.3.20.RELEASE
ResponseEntity<OtdsOauthToken> response = restTemplate.exchange(builder.toUriString(), HttpMethod.POST, entity, OtdsOauthToken.class); // Getting response object body as null, header is coming and Status is 200
Response body through Code :
Response body through Postman :
Upvotes: 1
Views: 3238
Reputation: 1892
Because you haven't yet shown the OtdsOauthToken
class, for now I can only guess. I think the problem may be because of you haven't set JSON-attribute names (with snack-case) for the fields or/and may be the class doesn't contain getters/setters.
So, the class must look like this:
public class OtdsOauthToken {
@JsonProperty("access_token")
private String accessToken;
private LocalDateTime expiration;
@JsonProperty("expires_in")
private Long expiresIn;
@JsonProperty("token_type")
private TokenType tokenType;
public String getAccessToken() {
return accessToken;
}
public void setAccessToken(String accessToken) {
this.accessToken = accessToken;
}
public LocalDateTime getExpiration() {
return expiration;
}
public void setExpiration(LocalDateTime expiration) {
this.expiration = expiration;
}
public Long getExpiresIn() {
return expiresIn;
}
public void setExpiresIn(Long expiresIn) {
this.expiresIn = expiresIn;
}
public TokenType getTokenType() {
return tokenType;
}
public void setTokenType(TokenType tokenType) {
this.tokenType = tokenType;
}
}
Also you can configure your ObjectMapper
and omit setting of attribute names.
this.objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
In this case the class might look as simple POJO-class:
public class OtdsOauthToken {
private String accessToken;
private LocalDateTime expiration;
private Long expiresIn;
private TokenType tokenType;
//getters ...
//setters ...
}
Upvotes: 2
Reputation: 1405
From what I see in the debug code, your OtdsOauthToken
class seems to have the field names as camelCase, while your response entity (in Postman) uses snake_case. That's why the RestTemplate
doesn't know to set the values on your object.
The solution would be to annotate your fields with @JsonProperty
, for example:
public class OtdsOauthToken {
@JsonProperty("access_token")
private String accessToken
@JsonProperty("token_type")
private String tokenType
@JsonProperty("expires_in")
private Integer expiresIn;
}
Upvotes: 1