我是 Spring MVC 的新手,我使用下面的代码从另一个应用程序获取响应。 4.3.20.发布
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
通过代码响应正文:
通过 Postman 响应正文:
最佳答案
因为您尚未显示 OtdsOauthToken
类,所以现在我只能猜测。我认为问题可能是因为您没有为字段设置 JSON 属性名称(带有零食大小写)或/并且可能是该类不包含 getters/setters。
因此,该类必须如下所示:
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;
}
}
您还可以配置您的ObjectMapper
并省略属性名称的设置。
this.objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
在这种情况下,该类可能看起来像简单的 POJO 类:
public class OtdsOauthToken {
private String accessToken;
private LocalDateTime expiration;
private Long expiresIn;
private TokenType tokenType;
//getters ...
//setters ...
}
关于java - 从 postman Spring Mvc 获取时未从resttemplate.exchange 获取响应正文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60429106/