如何为 Web 服务中可能的扩展准备业务对象
在创建 Web 服务时,我决定在客户端和 Web 服务之间交换业务对象 (BO)。
如果将来我收到扩展模型并在 BO 中添加一些新属性(字段)并将其发送给客户的请求,那么最好的方法是什么?
所以基本上,每个 BO 可能有 0..n 个元字段。
每个元字段都是类似 Key、Value 的,其中键可以是从简单数据类型到其他 BO 的任何内容。
这是一些用于建模 BO 的 Java 代码,我只需要确认我走在正确的轨道上。
class AbstractBO{
//optional list of meta fields for future extension
List<MetaField> metaFieldList;
//setters. getters
}
----
class MetaField {
private Object key;
private Object value;
// setters
// getters
}
----
class MyBO extends AbstractBO {
//BO specific fields
private String name;
...
}
---
TODAY
class Person extends AbstractBO {
private String name;
private int age;
//extend metaFieldList = null;
}
----
TOMORROW
class Person extends AbstractBO {
private String name;
private int age;
//list with new metafield
}
如何为明天的目的建模人物?
While creating a Web service I decided to exchange Business Object (BO) between client and web service.
If in the future I get a request to expand my model and put some new attributes (field) in my BO and send it to the client, what would be the best approach?
So basically, each BO may have 0..n meta-fields.
Each meta-field is Key,Value like, where keys can be anything from simple data types to other BOs.
Here is a little Java code for modelling BOs, I just need confirmation that I'm on the right track.
class AbstractBO{
//optional list of meta fields for future extension
List<MetaField> metaFieldList;
//setters. getters
}
----
class MetaField {
private Object key;
private Object value;
// setters
// getters
}
----
class MyBO extends AbstractBO {
//BO specific fields
private String name;
...
}
---
TODAY
class Person extends AbstractBO {
private String name;
private int age;
//extend metaFieldList = null;
}
----
TOMORROW
class Person extends AbstractBO {
private String name;
private int age;
//list with new metafield
}
How to model Person for Tomorrow purposes?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果,正如您的后续评论所暗示的那样,您实际上想要发送直接对象代码(大概是通过实现
Serialized
)而不是使用 XML 或 JSON(这是您在实现时通常会做的事情)网络服务),那么我不知道你实际上如何能够实现你想要的。当 Java 尝试通过反序列化来重新创建对象时,它必须将输入数据与它认为的类进行匹配。出于最佳实践目的,您应该实现
serialVersionUID
并在每次修改类时更改它,这样当您添加变量时,另一端的人将无法错误地重建该类如果他们有旧版本的代码,您可以发送给他们。If, as your follow-up comment implies, you actually want to send the direct object code (presumably by implementing
Serializable
) instead of using a XML or JSON (which is what you'd typically do when implementing a web service), then I don't know how you'd actually be able to achieve what you want.When Java tries to recreate your object by deserializing it, it will have to match the input data against what it believes the class to be. For best practice purposes, you should be implementing
serialVersionUID
and changing it each time you modify your class so that when you add variables, the person on the other end won't be able to erroneously reconstruct the class that you send them if they have an old version of the code.