Search code examples
jsonxml-parsingjaxbapache-camelpojo

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


how do you map JSON ( no root elem ) to XML using JAXB annotations? seems a common thing? has anyone experienced this too? I've searched.... thanks in advance!!!!

Karaf version 2.4.0.redhat-630310 Camel 2.17.0.redhat-630310

Trying to first authenticate, obtain the access_token and then use the access_token to gain access to the systems resources having authorization to use. how do you map JSON ( no root elem ) to XML using JAXB annotations?

here is the JSON response:

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

my POJO model 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;

...


Solution

  • Since I am only concerned with JSON, I will use Jackson Annotations. however, I would still like to understand how this would work using XML parsing. If anyone knows, please advice. thank you!

    so, this is my JSON mapping entity class supporting 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;
    }
    }