Java : 如何将 Oauth2 服务返回的 json 字符串映射到模型 class 对象
Java : How to map the json string returned by Oauth2 service to model class object
我有一个 oauth2 服务器,它将 return 一个包含访问令牌、刷新令牌及其有效性的 return 字符串,当带有授权类型、用户凭据和客户端凭据 comes.Below 的请求是我得到的响应字符串。
{"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 字符串中的所有这些字段映射到 class.How 我可以吗 that.I 需要将字段映射到下面的 class.
public class UserToken{
String accessToken;
Date accessTokenValidity;
String accessTokenType;
String refreshToken;
Date refreshTokenValidity;
String scope;
}
您可以使用 Jackson
库来做到这一点。
试试这个。 refreshToken
标签变成了 java 中的 class。
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;
}
}
我有一个 oauth2 服务器,它将 return 一个包含访问令牌、刷新令牌及其有效性的 return 字符串,当带有授权类型、用户凭据和客户端凭据 comes.Below 的请求是我得到的响应字符串。
{"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 字符串中的所有这些字段映射到 class.How 我可以吗 that.I 需要将字段映射到下面的 class.
public class UserToken{
String accessToken;
Date accessTokenValidity;
String accessTokenType;
String refreshToken;
Date refreshTokenValidity;
String scope;
}
您可以使用 Jackson
库来做到这一点。
试试这个。 refreshToken
标签变成了 java 中的 class。
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;
}
}