Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mapper.toBean()怎么指定使用Jackson来进行反序列化? #78

Open
mostcool opened this issue Aug 17, 2022 · 5 comments
Open

mapper.toBean()怎么指定使用Jackson来进行反序列化? #78

mostcool opened this issue Aug 17, 2022 · 5 comments

Comments

@mostcool
Copy link

mostcool commented Aug 17, 2022

       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
到底要怎么用?

@mostcool mostcool reopened this Aug 17, 2022
@troyzhxu
Copy link
Owner

你的用法示对的,如果得不到值,可以先把 响应报文体打印一下看看服务器端返回的是什么。

另外:

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);

@mostcool
Copy link
Author

你的用法示对的,如果得不到值,可以先把 响应报文体打印一下看看服务器端返回的是什么。

另外:

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}

@troyzhxu
Copy link
Owner

你使用的哪个版本?,如果不行就直接用 BodytoBean 方法:

HttpResult.Body body = result.getBody().cache();
Token token = body.toBean(Token.class);

@mostcool
Copy link
Author

你使用的哪个版本?,如果不行就直接用 BodytoBean 方法:

HttpResult.Body body = result.getBody().cache();
Token token = body.toBean(Token.class);

3.5.3

@troyzhxu
Copy link
Owner

嗯,这个版本的 MappertoBean 有点弱,下个版本会增强它,直接使用 HttpResult.BodytoBean 就行了。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants