map JSON 响应没有根元素到 POJO JAXB 绑定?

map JSON response having no root element to POJO JAXB binding?

如何使用 JAXB 注释将 JSON(无根元素)映射到 XML? 似乎很常见?有没有人也经历过这个?我已经搜索过....提前致谢!!!!

卡拉夫版本 2.4.0.redhat-630310 骆驼 2.17.0.redhat-630310

尝试首先进行身份验证,获取access_token,然后使用access_token获得对有权使用的系统资源的访问权限。如何使用 JAXB 注释将 JSON(无根元素)映射到 XML?

这是 JSON 响应:

{"access_token":"eyJhbGciOrZXkiLCJ0eXAiOiJKV1QifQ.","token_type": "bearer","refresh_token":"eyJhbGciXkiLCJ0eXAiOiJKV1QifQ.", "expires_in": 86399, "scope": "password.write openid","jti":75dcd85bd1247b4968f8802e54a9cc1"}

我的 POJO 模型class

package com.ge.dig.predix.entities;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Entities {

@XmlElement(name="access_token")
private String access_token;
@XmlElement(name="token_type")
private String token_type;
@XmlElement(name="refresh_token")
private String refresh_token;
@XmlElement(name="expires_in")
private String expires_in;
@XmlElement(name="scope")
private String scope;
@XmlElement(name="jti")
private String jti;

public String getAccessToken() {
    return access_token;
}
public void setAccessToken(String access_token) {
    this.access_token = access_token;
}
public String getTokenType() {
    return token_type;
}
public void setTokenType(String token_type) {
    this.token_type = token_type;

...

由于我只关心 JSON,我将使用 Jackson Annotations。但是,我仍然想了解使用 XML 解析如何工作。如果有人知道,请指教。谢谢!

所以,这是我的 JSON 映射实体 class 支持 JSON。

package com.myapp.entities;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonCreator;

@JsonIgnoreProperties({ "token_type", "scope", "jti" })
public class TokenEntities {

private String access_token;
private String token_type;
private String refresh_token;
private String expires_in;
private String scope;
private String jti;

@JsonCreator
public TokenEntities(@JsonProperty("access_token") String access_token,
                     //@JsonProperty("token_type")String token_type,
                     @JsonProperty("refresh_token")String refresh_token,
                     @JsonProperty("expires_in")int expires_in,
                     //@JsonProperty("scope")String scope,
                     //@JsonProperty("jti")String jti) {
    this.access_token = access_token;
    //this.token_type = token_type;
    this.refresh_token = refresh_token;
    this.expires_in = expires_in;
    //this.scope = scope;
    //this.jti = jti;
}

public String getAccessToken() {
    return access_token;
}

public String getTokenType() {
    return token_type;
}

public String getRefreshToken() {
    return refresh_token;
}

public int getExpires_in() {
    return expires_in;
}

public String getScope() {
    return scope;
}

public String getJti() {
    return jti;
}
}