Jak można użyć odbicia, aby uzyskać nazwy właściwości i wartości z POJO?

Więc piszę konwerter „POJO to JSON”. Chcę być w stanie przejśćList<T> obiekt i przekonwertować do JSON.

Mam nadzieję, że będzie to miało sens

/**
 *
     * NOT COMPLETE!!!  OBVIOUSLY!!!
 */
public abstract class Jsonator<T> implements Serializable {

    private Class<T> entityClass;
    private JSONObject json;
    private JSONArray jsonArray;

    public Jsonator(Class<T> entityClass) {
        this.entityClass = entityClass;
    }

    public void convert(List<T> paObjectList) throws IllegalArgumentException, IllegalAccessException {
        json = new JSONObject();
        jsonArray = new JSONArray();

        try {

            for (Object obj : paObjectList) {
                JSONObject objJson = new JSONObject();

                Class<?> kls = obj.getClass();

                Field[] fields = kls.getFields();
                for (Field field : fields) {
                    objJson.put(field.getName(), (T) field.get(obj));
                }

                jsonArray.add(objJson);
            }

            json.put("results", jsonArray);

        }
        catch (Exception ex) {
        }
    }

    public String error() {
        return "ERROR";
    }

    public String results() {
        if (json != null) {
            return json.toJSONString();
        }

        return "[]";
    }
}

Kiedy dotrę doObject obj sekcja, mojaobj jest poprawne. Mogę go zdebugować i zobaczyć nazwę i wartość klasy.

Powiedzmy, że ta klasa to:

public class User {
    private firstName;
    private lastName;

    ... getters....setters....etc...

}

Więc terazobj to witryna. OK, następnie próbuję pobrać nazwy pól (firstName, lastName), ale obiekt pól jest pusty.

Co ja robię źle?

Dzięki

EDYTOWAĆ

Mam to do roboty! To nie jest gotowy kod, ale teraz robi dokładnie to, czego chcę. Czytałem, że Google i Jackson też to zrobią. Jeśli ktoś może dostarczyć dobrego linku na temat selektywnego wybierania właściwości z POJO, to ja mam uszy.

Albo jeszcze lepiej, chciałbym to wiedziećCZEMU Nie powinienem tego robić w ten sposób?

Dzięki!

Jsonator (NOT FINISHED)

import java.io.Serializable;
import java.lang.reflect.Field;
import java.util.List;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

/**
 *
 * @author Cecil.Meeks
 */
public abstract class Jsonator<T> implements Serializable {

    private Class<T> entityClass;
    private JSONObject json;
    private JSONArray jsonArray;

    public Jsonator(Class<T> entityClass) {
        this.entityClass = entityClass;
    }

    public void convert(List<T> paObjectList) throws IllegalArgumentException, IllegalAccessException {
        json = new JSONObject();
        jsonArray = new JSONArray();

        try {

            for (Object obj : paObjectList) {
                JSONObject objJson = new JSONObject();

                Class<?> kls = obj.getClass();

                Field[] fields = kls.getDeclaredFields();
                for (Field field : fields) {
                    field.setAccessible(true);
                    objJson.put(field.getName(), field.get(obj));
                }

                jsonArray.add(objJson);
            }

            json.put("results", jsonArray);

        }
        catch (SecurityException ex) {
            ex.printStackTrace();
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public String error() {
        return "ERROR";
    }

    public String results() {
        if (json != null) {
            return json.toJSONString();
        }

        return "[]";
    }
}

Klasa witryny

import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import org.hibernate.annotations.GenericGenerator;

@Entity
@Table(name = "Sites")
public class Site implements Serializable {

    private String siteKey;
    private String site;
    private String siteType;
    private String address1;
    private String address2;
    private String city;
    private String zipCode;
    private String createdBy;
    private String glCode;

    public Site() {
    }

    @Id
    @GenericGenerator(name = "generator", strategy = "guid", parameters = {})
    @GeneratedValue(generator = "generator")
    public String getSiteKey() {
        return siteKey;
    }

    public void setSiteKey(String siteKey) {
        this.siteKey = siteKey;
    }

    @Column(name = "Site", unique = true, length = 125, nullable = false)
    public String getSite() {
        return site;
    }

    public void setSite(String site) {
        this.site = site;
    }

    @Column(name = "SiteType", unique = false, length = 8, nullable = true)
    public String getSiteType() {
        return siteType;
    }

    public void setSiteType(String siteType) {
        this.siteType = siteType;
    }

    @Column(name = "Address1", unique = false, length = 125, nullable = true)
    public String getAddress1() {
        return address1;
    }

    public void setAddress1(String address1) {
        this.address1 = address1;
    }

    @Column(name = "Address2", unique = false, length = 125, nullable = true)
    public String getAddress2() {
        return address2;
    }

    public void setAddress2(String address2) {
        this.address2 = address2;
    }

    @Column(name = "City", unique = false, length = 125, nullable = true)
    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    @Column(name = "ZipCode", unique = false, length = 50, nullable = true)
    public String getZipCode() {
        return zipCode;
    }

    public void setZipCode(String zipCode) {
        this.zipCode = zipCode;
    }

    @Column(name = "CreatedBy", unique = false, length = 125, nullable = true)
    public String getCreatedBy() {
        return createdBy;
    }

    public void setCreatedBy(String createdBy) {
        this.createdBy = createdBy;
    }

    @Column(name = "GLCode", unique = false, length = 11, nullable = true)
    public String getGlCode() {
        return glCode;
    }

    public void setGlCode(String glCode) {
        this.glCode = glCode;
    }


}

PRZYKŁAD

public class SiteJsonator extends Jsonator<Site> {

    public SiteJsonator() {
        super(Site.class);
    }

}

@Controller
@RequestMapping(value = "/sites")
public class SitesController {

    @Autowired
    private SiteService siteService;

    @RequestMapping(value = "/", method = RequestMethod.GET, headers = "Accept=application/json")
    @ResponseBody
    public String index(ModelMap map) {

        SiteJsonator list  = new SiteJsonator();;
        try {
            list.convert(siteService.getAll());
            return list.results();
        }
        catch (Exception ex) {
            return list.error();
        } 
    }
}

AKTUALIZACJA 2

Tutaj jest lepiejJsonator dla zainteresowanych:

https://gist.github.com/3893242

Możesz przekazać ciąg „exclude” [] i nie będzie go zawierał. Dodatkowo ma standard „wyniki, wiadomości itp.”, Które chcemy przekazać w naszych żądaniach AJAX. Dobre dla ExtJS.

questionAnswers(2)

yourAnswerToTheQuestion