【问题标题】:map JSON response having no root element to POJO JAXB binding?将没有根元素的 JSON 响应映射到 POJO JAXB 绑定?
【发布时间】:2018-09-01 08:10:31
【问题描述】:

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

Karaf 版本 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 模型类

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 xml-parsing jaxb apache-camel pojo


    【解决方案1】:

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

    所以,这是我支持 JSON 的 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;
    }
    }
    

    【讨论】:

      猜你喜欢
      • 2014-04-12
      • 2021-08-25
      • 2019-02-12
      • 1970-01-01
      • 2019-03-03
      • 1970-01-01
      • 2023-04-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多