繁体   English   中英

如何在wsimport创建的类中设置许多实例变量?

[英]How to set the many instance variables in a class created by wsimport?

我正在写一个基于SOAP的客户端,它将更新数据库中的字段。 由java wsimport实用程序创建的类之一包含表的表示,该表中的每个字段均作为受保护的实例变量。 例如:

public class CustomObject1Data {

@XmlElement(name = "ModifiedDate")
@XmlSchemaType(name = "dateTime")
protected XMLGregorianCalendar modifiedDate;
@XmlElement(name = "CreatedDate")
@XmlSchemaType(name = "dateTime")
protected XMLGregorianCalendar createdDate;
@XmlElement(name = "ModifiedById")
protected String modifiedById;
@XmlElement(name = "CreatedById")
protected String createdById;
@XmlElement(name = "ModId")
protected Integer modId;
@XmlElement(name = "Id")
protected String id;
@XmlElement(name = "CustomInteger5")
protected Integer customInteger5;
@XmlElement(name = "CustomInteger6")
protected Integer customInteger6;
.... 100+ more

用户界面将为这些字段提供友好名称。 他们熟悉的名称,因为它们是数据库中该记录的字段名称。 UI将在后台将UI字段名称映射到XML名称。 如果UI向我发送这样的查询字符串"CustomInteger5=some new value" ,我该如何设置该类成员?

@XmlElement(name = "CustomInteger5")   
    protected Integer customInteger5;

我知道我可以有一堆if语句,例如:

if(requestStr.fieldName.equals("CustomInteger5"){   
      setCustomInteger5(rquestStr.value);   
}

...

不过,这将是很多工作! 如果我将xml名称提供为String,可以动态设置这些类成员吗?

谢谢。

也许可以有一种更好的方法来完成我想做的事情,但是我能够使用Java反射API来完成我需要做的事情。 如果有人有更好的解决方案,我仍然对学习感兴趣吗? 这是我经过学习以了解反射的教程。 http://docs.oracle.com/javase/tutorial/reflect/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM