【问题标题】:Get the arraylist object from the model class using commons BeanUtils使用 commons BeanUtils 从模型类中获取 arraylist 对象
【发布时间】:2016-10-07 06:12:47
【问题描述】:

在我的模型类中,我有一个ArrayList 类型的私有字段,当我尝试使用BeanUtils 获取ArrayList 时,它没有说没有这种方法,有人可以解释一下这是为什么发生了什么?

代码如下:

public class ApplicationListDTO  implements DTO {  

  private Integer count = null;      
  private String next = null;      
  private String previous = null;      
  private List<ApplicationInfoDTO> list = new ArrayList<ApplicationInfoDTO>();
  private long lastUpdatedTime = 0L;
  private long createdTime = 0L;

  /**
  * gets and sets the lastUpdatedTime for ApplicationListDTO
  **/
  @org.codehaus.jackson.annotate.JsonIgnore
  public long getLastUpdatedTime(){
    return lastUpdatedTime;
  }
  public void setLastUpdatedTime(long lastUpdatedTime){
    this.lastUpdatedTime=lastUpdatedTime;
  }

  /**
  * gets and sets the createdTime for a ApplicationListDTO
  **/

  @org.codehaus.jackson.annotate.JsonIgnore
  public long getCreatedTime(){
    return createdTime;
  }
  public void setCreatedTime(long createdTime){
    this.createdTime=createdTime;
  }

  /**
   * Number of applications returned.\n
   **/
  @ApiModelProperty(value = "Number of applications returned.\n")
  @JsonProperty("count")
  public Integer getCount() {
    return count;
  }
  public void setCount(Integer count) {
    this.count = count;
  }

    /**
   * Link to the next subset of resources qualified.\nEmpty if no more resources are to be returned.\n
   **/
  @ApiModelProperty(value = "Link to the next subset of resources qualified.\nEmpty if no more resources are to be returned.\n")
  @JsonProperty("next")
  public String getNext() {
    return next;
  }
  public void setNext(String next) {
    this.next = next;
  }

    /**
   * Link to the previous subset of resources qualified.\nEmpty if current subset is the first subset returned.\n
   **/
  @ApiModelProperty(value = "Link to the previous subset of resources qualified.\nEmpty if current subset is the first subset returned.\n")
  @JsonProperty("previous")
  public String getPrevious() {
    return previous;
  }
  public void setPrevious(String previous) {
    this.previous = previous;
  }

    /**
   **/
  @ApiModelProperty(value = "")
  @JsonProperty("list")
  public List<ApplicationInfoDTO> getList() {
    return list;
  }
  public void setList(List<ApplicationInfoDTO> list) {
    this.list = list;
  }
}

方法调用代码如下:

Object object = ((ResponseImpl) message.getContent(List.class).get(0)).getEntity();
BeanUtils.getProperty(object,"list");

【问题讨论】:

    标签: java reflection jax-rs apache-commons-beanutils


    【解决方案1】:

    BeanUtils.getProperty(..)returns String,所以这不是你需要的。

    你可以在没有任何支持库的情况下做到这一点:

    try {
        Object object = ((ResponseImpl) message.getContent(List.class).get(0)).getEntity();
        Field field   = object.getClass().getDeclaredField("list");
    
        List<Object> list = field.get(object);
    
        [...]
    } catch (Exception e) {
        e.printStackTrace();
    }
    

    或者您可以使用来自Apache Commons Lang libraryFieldUtils 来完成。这是一个例子:

    try {
        Object object = ((ResponseImpl) message.getContent(List.class).get(0)).getEntity();
        Field field   = FieldUtils.getField(object.getClass(), "list", true);
    
        List<Object> list = field.get(object);
    
        [...]
    } catch (Exception e) {
        e.printStackTrace();
    }
    

    【讨论】:

      猜你喜欢
      • 2013-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-02
      • 2016-03-16
      • 2011-07-10
      • 2018-01-02
      • 1970-01-01
      相关资源
      最近更新 更多