0

我得到以下错误:

java.lang.NoSuchMethodException: Unknown property 'language' on class 'class java.lang.Class'

使用的休息电话:/Test/rest/Languages/insertobj/Japanese/Swedish%206

我正在使用:org.apache.commons.beanutils.PropertyUtils;

我的 Languages 类中确实有指定的属性“语言”,所以我不确定它为什么会抛出错误。当我这样声明它时它会起作用:

语言 lanobj = new Languages();

//语言类

public class Languages implements java.io.Serializable {
    private String language;
    private String displayName;

public Languages() {
        this.language = null;
        this.displayName = null;
    }


public Languages(String language, String displayName) {
    this.language = language;
    this.displayName = displayName;
}

public void setLanguageId(Long languageId) {
    this.languageId = languageId;
}

@Column(name = "language", nullable = false, length = 50)
public String getLanguage() {
    return this.language;
}

public void setLanguage(String language) {
    this.language = language;
}

@Column(name = "displayName", nullable = false, length = 50)
public String getDisplayName() {
    return this.displayName;
}

public void setDisplayName(String displayName) {
    this.displayName = displayName;
}

}

///资源管理器代码

@Path("{subResources}/insertobj/{language-id}/{displayname-id}")
//@POST 
@GET // Using get for the sake of making rest call with browser
@Produces(MediaType.TEXT_HTML)
public String getLanguageById(@PathParam("subResources") String id,
                            @PathParam("language-id") String language_id,
                            @PathParam("displayname-id") String displayname_id) 
{
    try{
        Class<?> c = Class.forName("com.test."+id);
        PropertyUtils.setProperty(c, "language",language_id);
        PropertyUtils.setProperty(c, "displayName",displayname_id);
        TransactionManager.createObject(c);     

    }catch (Exception exc){
        exc.printStackTrace();      
    }

}

//TransactionManager 片段:

public static void createObject(Object entityObject) {
    Transaction tx = null;
    //SessionFactoryUtil.getInstance().openSession();
    Session session =  SessionFactoryUtil.openSession();
    try {
      tx = session.beginTransaction();        
      session.save(entityObject);         
      tx.commit();
    } 
    catch (RuntimeException e)
    {
      if (tx != null && tx.isActive())
      {
        try 
        {
            // Second try catch as the rollback could fail as well
            tx.rollback();
        } 
        catch (HibernateException e1) {
            //logger.debug(“Error rolling back transaction”);
        }
        // throw again the first exception
        throw e;
      }
    }
    finally
    {
        session.close();
    }
  }
4

3 回答 3

5

您正在尝试为languageClass 对象设置属性"com.test."+id。您需要创建此类的一个实例来设置值。就像是:

try{
    Class<?> c = Class.forName("com.test."+id);
    Object o = c.newInstance();
    PropertyUtils.setProperty(o, "language",language_id);
    PropertyUtils.setProperty(o, "displayName",displayname_id);
    TransactionManager.createObject(o);     

}catch (Exception exc){
    exc.printStackTrace();      
}

引用的类c必须具有零参数构造函数才能使此代码正常工作。

于 2012-06-05T17:03:27.457 回答
1

setProperty在传递课程时需要一个实例,请参阅文档

Class<?> c = Class.forName("com.test."+id);
Language language = (Language)c.newInstance();
PropertyUtils.setProperty(language , "language",language_id);

要么你专门投到,Language要么你可以把它保留为Object

Object object  = Class.forName("com.test."+id).newInstance();
PropertyUtils.setProperty(object, "language",language_id);
于 2012-06-05T17:05:01.483 回答
0

由于您的类Languages没有定义默认构造函数,beanutils 无法进行反射。确保您的类中定义了默认构造函数。

于 2012-06-05T16:52:40.630 回答