okhttps
okhttps copied to clipboard
mapper.toBean()怎么指定使用Jackson来进行反序列化?
HTTP http = HTTP.builder()
.addMsgConvertor(new JacksonMsgConvertor())
.build();
HttpResult result = http.sync( "xxx")
.addUrlPara(paramMap)
.nothrow()
.post();
HttpResult.Body body = result.getBody().cache();
Mapper mapper = body.toMapper();
Token token = mapper.toBean(Token.class);
@Data
public class Token implements Serializable {
@JsonProperty("access_token")
private String accessToken;
@JsonProperty("refresh_token")
private String refreshToken;
@JsonProperty("expires_in")
private Long expiresIn;
}
Token token = mapper.toBean(Token.class); 不生效,得到token里的值都是null或0 到底要怎么用?
你的用法示对的,如果得不到值,可以先把 响应报文体打印一下看看服务器端返回的是什么。
另外:
HttpResult.Body body = result.getBody().cache();
Mapper mapper = body.toMapper();
Token token = mapper.toBean(Token.class);
// 以上三行代码等同于下面两行:
HttpResult.Body body = result.getBody().cache();
Token token = body.toBean(Token.class);
你的用法示对的,如果得不到值,可以先把 响应报文体打印一下看看服务器端返回的是什么。
另外:
HttpResult.Body body = result.getBody().cache(); Mapper mapper = body.toMapper(); Token token = mapper.toBean(Token.class); // 以上三行代码等同于下面两行: HttpResult.Body body = result.getBody().cache(); Token token = body.toBean(Token.class);
我这边返回的body如下: {"access_token":"fc97d9aee569767892dde98a2ce73485","refresh_token":"ea42e569be57dfce26df89ab562f76ba","expires_in":1800}
你使用的哪个版本?,如果不行就直接用 Body 的 toBean 方法:
HttpResult.Body body = result.getBody().cache();
Token token = body.toBean(Token.class);
你使用的哪个版本?,如果不行就直接用
Body的toBean方法:HttpResult.Body body = result.getBody().cache(); Token token = body.toBean(Token.class);
3.5.3
嗯,这个版本的 Mapper 的 toBean 有点弱,下个版本会增强它,直接使用 HttpResult.Body 的 toBean 就行了。