Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java:如何将Oauth2服务返回的json字符串映射到模型类对象_Java_Json_Model_Jackson_Spring Security Oauth2 - Fatal编程技术网

Java:如何将Oauth2服务返回的json字符串映射到模型类对象

Java:如何将Oauth2服务返回的json字符串映射到模型类对象,java,json,model,jackson,spring-security-oauth2,Java,Json,Model,Jackson,Spring Security Oauth2,我有一个oauth2服务器,当带有granttype、用户凭据和客户端凭据的请求出现时,它将返回一个json字符串,其中包含访问令牌、刷新令牌及其有效性 {"value":"yu592e04-o9d5-8724-92a8-c5034df13cae","expiration":"Jul 25, 2016 4:14:31 PM","tokenType":"bearer","refreshToken":{"expiration":"Sep 24, 2016 3:14:31 PM","value":"b

我有一个oauth2服务器,当带有granttype、用户凭据和客户端凭据的请求出现时,它将返回一个json字符串,其中包含访问令牌、刷新令牌及其有效性

{"value":"yu592e04-o9d5-8724-92a8-c5034df13cae","expiration":"Jul 25, 2016 4:14:31 PM","tokenType":"bearer","refreshToken":{"expiration":"Sep 24, 2016 3:14:31 PM","value":"bb6b7d65-a938-h75b-9cc5-d78b38e7adf9"},"scope":[],"additionalInformation":{}}
现在,我需要将json字符串中的所有字段映射到一个类。我如何才能做到这一点。我需要将字段映射到下面的类

public class UserToken{
String accessToken;
Date accessTokenValidity; 
String accessTokenType;
String refreshToken;
Date refreshTokenValidity;
String scope;
} 

您可以使用
Jackson
library来实现这一点

试试这个。
refreshtToken
标记成为java中的一个类

public class Convertor {

    public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException {
        String json = "{\"value\":\"yu592e04-o9d5-8724-92a8-c5034df13cae\",\"expiration\":\"Jul 25, 2016 4:14:31 PM\",\"tokenType\":\"bearer\",\"refreshToken\":{\"expiration\":\"Sep 24, 2016 3:14:31 PM\",\"value\":\"bb6b7d65-a938-h75b-9cc5-d78b38e7adf9\"}}";
        Convertor converter = new Convertor();
        UserToken token = converter.fromJson(json);
        System.out.println(token);

    }

    public UserToken fromJson(String json) throws JsonParseException, JsonMappingException, IOException {
        UserToken token = (UserToken) new ObjectMapper().readValue(json, UserToken.class);

        return token;
    }

}

class UserToken {

    String value;
    String expiration;
    String tokenType;
    RefreshToken refreshToken;

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public String getExpiration() {
        return expiration;
    }

    public void setExpiration(String expiration) {
        this.expiration = expiration;
    }

    public String getTokenType() {
        return tokenType;
    }

    public void setTokenType(String tokenType) {
        this.tokenType = tokenType;
    }

    public RefreshToken getRefreshToken() {
        return refreshToken;
    }

    @JsonProperty("refreshToken")
    public void setRefreshToken(RefreshToken refreshToken) {
        this.refreshToken = refreshToken;
    }



    @Override
    public String toString() {

        return "value " + value + "expiration " + expiration + "refreshToken.Expiration " + refreshToken.getExpiration()
                + " refreshToken.getValue: " + refreshToken.getValue();
    }
}

class RefreshToken {
    String expiration;
    String value;

    public String getExpiration() {
        return expiration;
    }

    public void setExpiration(String expiration) {
        this.expiration = expiration;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

}

我用我需要映射这些字段的类更新了这个问题。我不认为我们的exmaple对我有帮助,因为在我提供的json字符串中有两个字段的标签是“value”和“expiration”refreshtoken字段还包含两个子字段,我需要将它们分开。第一个值字段应该映射到accessToken,第二个值字段映射到refreshtoken。类似地,第一个过期字段应该映射到accessTokenValidity,第二个映射到refreshTokenValidity。u的任何解决方案都可以显示jackson的gradle插件吗这里用的图书馆?看看这个家伙