编程语言
首页 > 编程语言> > java-(太多)复杂对象的宁静架构问题

java-(太多)复杂对象的宁静架构问题

作者:互联网

好了,我已经负责了RESTful架构的服务器和客户端(内部使用)部分. (使用restlet).

我们有一个公开Post操作的资源.这是一个简化的版本:

public class UserResource {

    @Post
    public Representation create(UserRegistration registration) {
        SomeService.getInstance().createUser(registration);
        return new XstreamRepresentation(new RegistrationResponse(registration.getUniqueCode()););
    }

几个月来,我们一直是唯一使用这些服务的公司,因此域对象在客户端和服务器端之间共享…并且运行良好.

现在我们必须记录这些资源,并让其他客户端使用它们,因此出现了一些“问题”,这使我认为此API可能有点太复杂了.

例如,此邮政服务.
内部方法接受复杂类型的UserRegistration

public class UserRegistration implements Serializable {

    private Profile profile;
    private Boolean someBooleanProperty;

    public UserRegistration(Profile profile) {
        this(profile, true);
    }

    public Profile getProfile() {
        return profile;
    }

    public boolean isSomeBooleanProperty() {
        return someBooleanProperty;
    }

}

依次使用另一个复杂对象(配置文件)

public class Profile {

    private String nickname;
    private String email;
    private String password;
    private String firstname;
    private String lastname;
    private Date birthDate;
    private String phone;
    private Address address;
    private GenderType gender;
    private String subscriptionSite;
    private Date privacyAcceptanceDate;
    private Date subscriptionDate;
    private String activationCode;
    private String socialSecurityNumber;
    ...

这使用了很多复杂的类型等等.

复杂类型的这种使用确实使我感到困扰.
我要么不知道该如何记录(除了为这些复杂对象的内部属性制作一长串清单),否则我迷路了.

我的问题是:
我必须简化吗?
这个架构设计得很糟糕吗?
几种构造方法会成功吗?

解决方法:

通过在客户端和服务器之间共享域实体类型,您(不是专门说您)完全摆脱了REST的观点. RESTful系统应该仅共享媒体类型和链接关系.使用SOAP可以像您一样轻松地共享类型,因为WSDL允许工具包处理使客户端和服务器类型保持同步的细节.

REST就是要减少客户端和服务器之间的耦合,以使它们独立发展.显然,如果您有大量的共享类型,那就很难了,这就是为什么您目前有这种不好的感觉.

我对这个问题采取的解决方案是定义两种媒体类型.一种是通用实体数据容器.我们将其称为BusinessDocument,另一个称为BusinessLayout.客户端使用BusinessDocument从服务器检索所有数据,而BusinessLayout提供“数据绑定”信息,因此客户端知道在我的UI中何处显示不同的业务数据.

通过这样做,我能够构建一个真正不了解正在处理的数据细节的客户端,它只知道如何在UI上显示它以便用户进行交互.通过这样做,我可以使用一种媒体类型来描述数百个不同的业务实体.

标签:restlet,rest,restful-architecture,java
来源: https://codeday.me/bug/20191208/2090083.html