Spring boot中根据activeProfiles为类设置serialVersionUID

问题描述 投票:0回答:1

我有一个类实现了 Serialized 接口,它基本上充当 DTO。现在这个类是 ind 分支的一部分,用作 Web 应用程序的测试版本。还有另一个分支 aus ,用作生产应用程序。生产应用程序只是遗留代码,功能较少。作为长期的一部分,我们正在尝试转向单一代码库。因此,基本上测试版本和生产应用程序将使用 ind 分支的代码,该代码似乎具有最新功能。为此,我们添加了除默认值之外的多个 application.properties 文件,通过 jenkins 使用环境变量设置活动配置文件。

现在,ind 分支代码和 aus 分支代码都有这个 DTO 类。在aus代码中,它有一个手动指定的serialVersionUID。 ind分支中,不是手动指定的,是根据字段决定的。现在,作为将 aus 分支的后端代码移动到 ind 分支代码的一部分,返回对象列表的 API 调用会在本地类和流类之间抛出序列化不匹配错误。

我希望这两个应用程序现在能够单独运行。因此,当使用 speing.profiles.active 切换环境或活动配置文件时,serialVersionUID 需要像以前两个应用程序独立时一样进行切换。我尝试通过 System.getProperty() 获取当前活动配置文件,然后检查它们,并根据配置文件设置serialVersionUID。

然后,Spring boot 应用程序似乎在启动过程中显示,因为配置文件设置正确,但是当我尝试在类中设置 activeProfile 值时,它为空。还尝试添加 @Configuration 文件并通过 @Value() 获取活动配置文件。 EveryThing 在应用程序启动之前一直有效,但在执行返回对象的 API 调用时显示 null。

@Configuration
public class ActiveProfileConfiguration {

    @Value("${spring.profiles.active}")
    String activeProfile;
    
     public boolean isInd() {
        System.out.println("active : "+activeProfile);
        return "in-test-local".equals(activeProfile);
    }

    public boolean isAus() {
        System.out.println("active : "+activeProfile);
        return "au-test-local".equals(activeProfile);
}

这些是为了根据当前活动配置文件获取动态serialVersionUID而进行的最新更改。

@Getter
@Setter
@ApiModel(value = "Organization", description = "Capture the information about the data of Organization")
public class Organization implements Serializable {
    
    private static final long serialVersionUID;

    static {
        ActiveProfileConfiguration profileConfiguration = new ActiveProfileConfiguration();

        if(profileConfiguration.isInd()) {
            serialVersionUID = 74845715165655L;
            System.out.println("Ind");
        }
        else if(profileConfiguration.isAus()) {
            serialVersionUID = -8450977346456L;
            System.out.println("Aus");
        }
        else {
            serialVersionUID = 1L;
            System.out.println("else..");
        }
    }

String id;
    @NotNull
    private String name;
    private Media logo;
    private Media advPic;
    private String website;
    @NotNull
    private String telephone;
    @NotNull
    private String businessCategoryId;
    @ReadOnlyProperty
    private OrganizationPreferences preferences;
    private SuperAdminOrganizationControl superAdminControl;
    private Constants.OrganizationMode orgMode;
    @NonNull
    private String userSub;
    private String geoLocationId;
    private GeoLocation location;
    private String description;
    private String orgRef;
    private String orgUrl;
    private String orgEmail;
    private boolean superAdminEnable;
    @NotNull
    private String whatsAppNumber;
    private String privacyStatement;
    
    private Boolean isCallback;
    private String callbackId;
    //Only in response, not database
    private List<SelectOption> serviceCategoryList = new ArrayList<SelectOption>();

    List<ProviderServiceHours> workingHours = new ArrayList<>();

}

我在这里缺少什么?是否有其他方法可以让同一个类在不同环境下顺利运行?

java spring spring-boot serialization deserialization
1个回答
0
投票

我想到的问题是:serialVersionUUID 是潜在问题的症状吗?听起来代码是在说“如果我们在分支 X,则执行...”。这并不常见。我希望在构建过程中将目标指定为测试网络应用程序、生产遗留版本等。顺便说一句,我发现很难理解您想要什么目标。然后,在构建过程中,根据目标检索依赖项/包,并获得为目标量身定制的二进制文件。通常这是由 Maven 完成的,然后从 Maven 存储库(本地)检索。您的设置如何?然后,maven 存储库中的项目有不同的依赖项。例如 our-project.war、our-project-development.jar、our-project-test.jar。那么 DTO 的一个版本可以位于 -product.jar 中,另一个版本可以位于 -test.jar 中。构建配置根据指定的目标检索正确的 jar。 IDE 支持这种设置。

您的构建流程是如何组织的?你使用maven,还是其他工具?

您的迁移策略是什么?

根据您的故事,作为迁移策略,我会考虑将整个 ind 代码库移动到 com.ourcompany.ind.* 包。然后将整个 aus 代码库放入 com.ourcompany.aus.* 包中。然后让构建过程根据目标选择代码库之一。然后通过将代码从 ind/aus 包中移出来逐渐消除重复代码。该代码最终将出现在 our-project.war 中。

© www.soinside.com 2019 - 2024. All rights reserved.